[an error occurred while processing this directive]
$name = new PDO("dbprogram:dbname=database;host=server", username, password);
$name->query("SQL query");
# connect to world database on local server
$db = new PDO("mysql:dbname=world;host=localhost", "traveler", "packmybags");
$db->query("SELECT * FROM countries WHERE population > 100000000;");
mysql_connectquery function returns rows that match a queryquery
$db = new PDO("dbprogram:dbname=database;host=server", username, password);
$rows = $db->query("SQL query");
foreach ($rows as $row) {
do something with $row;
}
query returns all result rows
$row["population"] gives the value of the population column
$db = new PDO("mysql:dbname=imdb_small", "jessica", "guinness");
$rows = $db->query("SELECT * FROM actors WHERE last_name LIKE 'Del%'");
foreach ($rows as $row) {
?>
<li> First name: <?= $row["first_name"] ?>,
Last name: <?= $row["last_name"] ?> </li>
<?php
}
| name | description |
|---|---|
query
|
performs a SQL SELECT query on the database |
exec
|
performs a SQL query that modifies the database (INSERT, DELETE, UPDATE, etc.) |
getAttribute,setAttribute
|
get/set various DB connection properties |
quote
|
encodes a value for use within a query |
# get query parameter for name of movie
$title = $_GET["movietitle"];
$rows = $db->query("SELECT year FROM movies WHERE name = '$title'");
# get query parameter for name of movie
$title = $_GET["movietitle"];
$title = $db->quote($title);
$rows = $db->query("SELECT year FROM movies WHERE name = $title");
quote method on any variable to be insertedquote escapes any illegal chars and surrounds the value with ' quotes
$db = new PDO("mysql:dbname=imdb_small", "jessica", "guinness");
$rows = $db->query("SEEELECT * FROM movies WHERE year = 2000"); # FALSE
FALSE or NULL
$db = new PDO("mysql:dbname=imdb_small", "jessica", "guinness");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$rows = $db->query("SEEELECT * FROM movies WHERE year = 2000"); # kaboom!
setAttribute, you can tell PDO to throw (generate) a PDOException when an error occurs
try {
statement(s);
} catch (ExceptionType $name) {
code to handle the error;
}
try/catch statement attempts to run some code, but if it throws a given kind of exception, the program jumps to the catch block and runs that code to handle the error
try {
$db = new PDO("mysql:dbname=imdb_small", "jessica", "guinness");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$rows = $db->query("SEEELECT * FROM movies WHERE year = 2000");
foreach ($rows as row) { ... }
} catch (PDOException $ex) {
?>
<p>Sorry, a database error occurred. Please try again later.</p>
<p>(Error details: <?= $ex->getMessage() ?>)</p>
<?php
}
The $rows variable returned by PDO's query method is technically not an array but an object of type PDOStatement.
It can be foreach-ed over like an array, but it also has the following methods:
columnCount()
|
number of columns in the results |
fetch()
|
return the next row from the results |
fetchColumn(number)
|
return the next column from the results |
rowCount()
|
number of rows returned by the query |
$rows = $db->query("..."); # query omitted
if ($rows->rowCount() > 0) {
$first_row = $rows->fetch();
...
}
SHOW DATABASES; USE database; SHOW TABLES;
$ mysql -u yourusername -p Password: Welcome to the MySQL monitor. Commands end with ; or \g. mysql> USE world; Database changed mysql> SHOW TABLES; +-----------+ | cities | | countries | | languages | +-----------+ 3 rows in set (0.00 sec)
[stepp@webster ~]$ mysql -u myusername -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. mysql> use imdb_small; Database changed mysql> select * from actors where first_name like '%mick%'; +--------+------------+-----------+--------+ | id | first_name | last_name | gender | +--------+------------+-----------+--------+ | 71699 | Mickey | Cantwell | M | | 115652 | Mickey | Dee | M | | 470693 | Mick | Theo | M | | 716748 | Mickie | McGowan | F | +--------+------------+-----------+--------+ 4 rows in set (0.01 sec)