Database Reference
In-Depth Information
The
bind_result()
prepares thevariables that will be used to parse the array ele-
ments, or fields of the results. Calling on the statement handle again, a
while
statement
loops through the results using the
fetch()
function to fetch data one row at a time
from the results. Within the
while
statement block, we're printing the values with
HTML tags. When it's finished, we close the statement handle and the connection.
The output of this script is a line for each bird based on the search criteria in the
birds
table. In this simple example, only a few of the many PHP functions for MySQL are used
to get and display data. These snippets are shown here together within a very basic web
page:
<html>
<body>
<?
php
$search_parameter = $_REQUEST[
'birdname'
];
$host =
'localhost'
;
$user =
'public_api'
;
$pw =
'pwd_123'
;
$db =
'rookery'
;
$connect =
new
mysqli
($host, $user, $pw, $db);
if
(
mysqli_connect_errno
()) {
printf
(
"Connect failed: %s
\n
"
,
mysqli_connect_error
());
exit
();
}
?>
<h3>
Birds -
<?php echo $search_parameter ?>
</h3>
<p>
Below is a list of birds in our database based on your search
criteria:
</p>
<?
php
$sql_stmnt =
"SELECT common_name, scientific_name
FROM birds
WHERE common_name LIKE ?"
;
$sth = $connect->
prepare
($sql_stmnt);
$search_parameter =
"%"
. $search_parameter .
"%"
;
$sth->
bind_param
(
's'
, $search_parameter);
$sth->
execute
();
$sth->
bind_result
($common_name, $scientific_name);
while
($sth->
fetch
()) {