There are a couple of examples in the manual that could help you with that ; for instance, on the manual page of mysql_query
(quoting, and adapting a bit) :
You first have to execute the query :
$result = mysql_query('SELECT * from mytable');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Note that dying in case of an error, and echoing the error-message, is OK while developping -- but you shouldn't do that in a production environment !
And, then, you have to loop over the lines of results, fetching them one at a time :
while ($row = mysql_fetch_assoc($result)) {
// Use the data in $row
}
And, inside this loop, as $row
is an array, you can iterate over its content with a foreach
loop :
foreach ($row as $name => $value) {
echo "column $name contains $value<br />";
}
Note : at this point, you should really invest some time going through a couple of sections of the PHP manual : it will take some time, yes ; but it will definitely help you, and will not be wasted time :-)