Aims: to learn how to write a PHP script that
while
loops<?php // Try to connect to database $dbconnection = mysql_connect($host, $user, $password); if ( ! $dbconnection ) { die('Unable to connect!'); } // Try to select database $dbselection = mysql_select_db($dbname); if ( ! $dbselection ) { die('Unable to select database'); } // Query the database $sql = "SELECT * FROM birds;"; $dbresult = mysql_query($sql); if ( ! $dbresult ) { die('Error in query ' . mysql_error()); } // Display the results of the query if (mysql_num_rows($dbresult) == 0) { echo 'No rows found'; } else { // Output each row of query results while ($row = mysql_fetch_assoc($dbresult)) { echo "<p>$row[num] $row[name] $row[species]</p>"; } } // Free up memory and close the connection mysql_free_result($dbresult); mysql_close($dbconnection); ?>
$dbconnection = mysql_connect($host, $user, $password);
$host
will be 'cosmos.ucc.ie'
$user
will be your usual CS id, e.g. 'dtab1'
$password
will not be your normal Windows/Unix password(s); it will
be the password we sent in an email before Christmasmysql_connect
returns a 'link identifier' or
false if a connection cannnot be made
$dbconnection = mysql_connect($host, $user, $password); if ( ! $dbconnection ) { die('Unable to connect!'); }
if ( ! $dbconnection = mysql_connect($host, $user, $password) ) { die('Unable to connect!'); }
$dbconnection = mysql_connect($host, $user, $password) or die('Unable to connect!');
$dbselection = mysql_select_db($dbname); if ( ! $dbselection ) { die('Unable to select database'); }
$dbname
) will be the name we sent you in an
email just before Christmas
$sql = "SELECT * FROM birds;";
mysql_query
function:
$dbresult = mysql_query($sql);
if ( ! $dbresult ) { die('Error in query ' . mysql_error()); }The function
mysql_error
returns a string describing the most
recent error
$dbresult
if (mysql_num_rows($dbresult) == 0) { echo 'No rows found'; } else { ... }
mysql_fetch_assoc
function,
if (mysql_num_rows($dbresult) == 0) { echo 'No rows found'; } else { while ($row = mysql_fetch_assoc($dbresult)) { ...do something with $row... } }
mysql_fetch_assoc
returns each row as
an associative array$row
will contain:
('num' => 1, 'name' => 'Stevie', 'species' => 'swan')
$row['num']
, $row['name']
and
$row['species']
:
if (mysql_num_rows($result) == 0) { echo 'No rows found'; } else { while ($row = mysql_fetch_assoc($result)) { echo "<p>{$row['num']} {$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:
mysql_free_result($dbresult);
mysql_close($dbconnection);
$snum
, $bnum
and $donation
contain values, e.g. from a form, and that we have validated them
<?php // Try to connect to database $dbconnection = mysql_connect($host, $user, $password); if ( ! $dbconnection ) { die('Unable to connect!'); } // Try to select database $dbselection = mysql_select_db($dbname); if ( ! $dbselection ) { die('Unable to select database'); } // Insert into the database $sql = "INSERT INTO donations (snum, bnum, donation) VALUES ({$snum}, {$bnum}, {$donation});"; $dbresult = mysql_query($sql); if ( ! $dbresult ) { die('Error in query ' . mysql_error()); } // Close the connection mysql_close($dbconnection); ?>
$dbresult
does not contain a result setmysql_num_rows
, mysql_fetch_assoc
and
mysql_free_result