Aims: to learn how to write a PHP script that
while loopsAssume the Nerdland Bird Sanctuary database has been created and populated with data
// Try to connect to database
$dbconnection = mysqli_connect( $host, $user, $password, $dbname );
if ( ! $dbconnection )
{
die('Unable to connect to database');
}
// Query the database
$sql = "SELECT * FROM birds;";
$dbresult = mysqli_query( $dbconnection, $sql );
if ( ! $dbresult )
{
die('Error in query ' . mysqli_error( $dbconnection ));
}
// Display the results of the query
if ( mysqli_num_rows( $dbresult ) == 0 )
{
echo "<p>No rows found</p>";
}
else
{
// Output each row of query results
while ( $row = mysqli_fetch_assoc( $dbresult ) )
{
echo "<p>{$row['bnum']} {$row['name']} {$row['species']}</p>";
}
}
// Free up memory and close the database connection
mysqli_free_result( $dbresult );
mysqli_close( $dbconnection );
$dbconnection = mysqli_connect( $host, $user, $password, $dbname );
$host will be 'localhost'
$user will be your usual CS id, e.g. 'dtab6'
(or probably 'root' at home)
$password will not be your normal
Windows/Unix password(s); it will
be the password you used in CS1106
(or whatever you chose when configuring MySQL at home)
$dbname will be the name of your database — the one
you used in CS1106, e.g. '2016_dtab6'
(or whatever you used when creating the database at home)
mysqli_connect returns a 'link identifier' or
false if a connection cannot be made
$dbconnection = mysqli_connect( $host, $user, $password, $dbname );
if ( ! $dbconnection )
{
die('Unable to connect!');
}
die function causes its parameter to be echoed and then
causes execution of the script to terminate
if ( ! $dbconnection = mysqli_connect( $host, $user, $password, $dbname ) )
{
die('Unable to connect!');
}
($dbconnection = mysqli_connect( $host, $user, $password, $dbname )) ||
die('Unable to connect!');
$sql = "SELECT * FROM birds;";
mysqli_query function:
$dbresult = mysqli_query( $dbconnection, $sql );
false if the query fails:
if ( ! $dbresult )
{
die('Error in query ' . mysqli_error( $dbconnection ));
}
The function mysqli_error returns a string describing the most
recent error
$dbresult
if ( mysqli_num_rows( $dbresult ) == 0 )
{
echo '<p>No rows found</p>';
}
else
{
…
}
mysqli_fetch_assoc function,
NULL, when there are no more rows
if ( mysqli_num_rows( $dbresult ) == 0 )
{
echo "<p>No rows found</p>";
}
else
{
while ( $row = mysqli_fetch_assoc( $dbresult ) )
{
…do something with $row…
}
}
while-loop seems most appropriate: unbounded repetition
for-loop: bounded repetition
$num_of_rows = mysqli_num_rows( $dbresult );
if ( $num_of_rows == 0 )
{
echo "<p>No rows found</p>";
}
else
{
for ( $i = 0; $i < $num_of_rows; $i++ )
{
$row = mysqli_fetch_assoc( $dbresult );
…do something with $row…
}
}
mysqli_fetch_assoc returns each row as
an associative array
$row
might contain:
(
'bnum' => 1,
'name' => 'Stevie',
'species' => 'swan'
)
$row['bnum'], $row['name'] and
$row['species']:
if ( mysqli_num_rows( $dbresult ) == 0 )
{
echo "<p>No rows found</p>";
}
else
{
while ( $row = mysqli_fetch_assoc( $dbresult ) )
{
echo "<p>{$row['bnum']} {$row['name']} {$row['species']}</p>";
}
}
$dbresult) may occupy a lot of memory.
Once you've finished with the results of the query, you can free the
memory:
mysqli_free_result( $dbresult );
mysqli_close( $dbconnection );
Imagine that $snum, $bnum and
$donation contain values, e.g. from a form
// Try to connect to database
$dbconnection = mysqli_connect( $host, $user, $password, $dbname );
if ( ! $dbconnection )
{
die('Unable to connect to database');
}
// Insert into the database
$sql = "INSERT INTO donations (snum, bnum, donation)
VALUES ({$snum}, {$bnum}, {$donation});";
$dbresult = mysqli_query( $dbconnection, $sql );
if ( ! $dbresult )
{
die('Error in query ' . mysqli_error( $dbconnection ));
}
// Close the connection
mysqli_close( $dbconnection );
$dbresult does not contain a result set
mysqli_num_rows, mysqli_fetch_assoc and
mysqli_free_result