<?
$data = “SELECT * FROM vituopen WHERE lighting”;
$data = @mysql_query(‘SELECT * FROM lighting’);
if (!$data);
$info = mysql_fetch_array( $data );
Print "<b>Name:</b> ".$info[‘Cast’] . " ";
Print "<b>Characters:</b> “.$info[‘Characters’] . " <br>”;
?>
This script pulls only the data from the first row in the table of the database and not from all the row in the table. So how do I change this script to fetch data from the the entire table in the database?
When you want to grab all the inserted values you either need to use a while or foreach loop to go through the array. Below i have listed some things which is just friendly feedback.
- When using MySQL queries even if a query works its best practice to write an error statement like i have in the example below as it helps for debugging if something changes.
- When using PHP reserved names such as “print” and “echo” its best to use the lowercase name as that is what 99% of all IDE’s are programmed to see and personally its just easier to read.
Here is an example of how you could use your code
//$data = "SELECT * FROM vituopen WHERE lighting";
if (!$data = mysql_query('SELECT * FROM lighting')) {
die('MySQL Error: ' . mysql_error());
}
while ($info = mysql_fetch_array($data)) {
echo '<b>Name:</b> ' . $info['Cast'] . ' ';
echo '<b>Characters:</b> ' . $info['Characters'] . '<br /><br />';
}