A sample connection to a MySQL database using the library database described
earlier might look like the following code snippet. Keep in mind that this example
uses the object-oriented interface for mysqli.
Código PHP:
Ver original$mysqli = new mysqli(’localhost’, ’dbuser’, ’dbpass’, ’library’);
echo ’Connect failed: ’ . mysqli_connect_error();
}
// All other database calls go here
$mysqli->close();
The same connection using a procedural approach instead of OOP might look
like this. Note that there are not many differences, the main one being the use
of the database resource in the subsequent mysqli_* function calls, such as in
mysqli_close() here.
Código PHP:
Ver original$dbh = mysqli_connect(’localhost’, ’dbuser’, ’dbpass’, ’library’);
if (!$dbh) {
echo ’Connect failed: ’ . mysqli_connect_error();
}
// All other database calls go here
// Filter input from $_GET
$author = ’’;
{
$author = $_GET[’author’];
}
// Escape the value of $author with mysqli->real_escape_string()
$sql = ’SELECT author.*, book.* FROM author
LEFT
JOIN book ON author
.id
= book
.author_id
WHERE author.last_name = ’ . $mysqli->real_escape_string($author);
// Execute the statement and echo the results
if (!$mysqli->real_query($sql)) {
echo ’Error in query: ’ . $mysqli->error;
}
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_assoc())
{
echo "{$row[’title’]}, {$row[’last_name’]}\n";
}
$result->close();
}
For the procedural style, the code would look like this:
// Filter input from $_GET
$author = ’’;
{
$author = $_GET[’author’];
}
Database Programming ” 165
// Escape the value of $author with mysqli->real_escape_string()
$sql = ’SELECT author.*, book.* FROM author
LEFT
JOIN book ON author
.id
= book
.author_id
WHERE author.last_name = ’ . mysqli_real_escape_string($dbh, $author);
// Execute the statement and echo the results
echo ’Error in query: ’ . mysqli_error();
}
{
echo "{$row[’title’]}, {$row[’last_name’]}\n";
}
}
Prepared Statements and Bound Parameters With mysqli
// Filter input from $_GET
$author = ’’;
{
$author = $_GET[’author’];
}
// Set a named placeholder in the SQL statement for author
$sql = ’SELECT book.title FROM author
LEFT
JOIN book ON author
.id
= book
.author_id
WHERE author.last_name = ?’;
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param(’s’, $author);
$stmt->execute();
$stmt->bind_result($title);
while ($stmt->fetch()) {
echo "{$title}, {$author}\n";
}
$stmt->close();
}
Again, the same code using the procedural approach looks like this:
// Filter input from $_GET
$author = ’’;
{
$author = $_GET[’author’];
}
// Set a named placeholder in the SQL statement for author
$sql = ’SELECT book.title FROM author
LEFT
JOIN book ON author
.id
= book
.author_id
WHERE author.last_name = ?’;
echo "{$title}, {$author}\n";
}
}
Transactions With mysqli
$mysqli->autocommit(FALSE);
$mysqli->query("INSERT INTO book (isbn, title, author_id, publisher_id)
VALUES (’0395974682’, ’The Lord of the Rings’, 1, 3)");
$mysqli->query("INSERT INTO book (title) VALUES (’Animal Farm’, 3, 2)");
if (!$mysqli->commit()) {
$mysqli->rollback();
}
The procedural version of the code is very similar:
mysqli_query($dbh, "INSERT INTO book (isbn, title, author_id, publisher_id) VALUES (’0395974682’, ’The Lord of the Rings’, 1, 3)");
mysqli_query($dbh, "INSERT INTO book (title) VALUES (’Animal Farm’, 3, 2)");
}