mysql_fetch_array() returns an array with one table row in it.
Code: Select all
<?php
while ($allcerts = mysql_fetch_array($sql_query)) {
echo ('<p align="center">'.$allcerts[0].' <br>');
}
?>
The while loop takes whatever is in it's parentheses and checks whether it is TRUE or FALSE. In PHP anything that is not zero is TRUE. That means that when
$allcerts = mysql_fetch_array($sql_query) returns a row and puts it in $allcerts, the boolean expression in the while loop is evaluated to TRUE.
The result is that the while statement loops through all rows returned by the SELECT query. One at a time.
At this point I should tell you that $allcerts is a bad name for its mission. It should be called $certRow or whatever, but not "all certs", because it will only get one cert for each mysql_fetch_array() call.
Then you take a look at the echo statement and see that the $allcerts is an array and used as such;
$allcerts[0]
This means that your "certs.CERTS" is accessible withing the while loop using $allcerts[0].
IF you had more columns in your query, IE:
Code: Select all
<?php
$sql_query = @mysql_query("SELECT certs.ID, certs.CERTS FROM certs, faccerts WHERE faccerts.FID='$fid' AND certs.ID=faccerts.CID");
?>
then certs.ID for the cert would be accessible with $allcerts[0] and certs.CERTS for the cert would be accessible with $allcerts[1].
That [0] and [1] refers to the "columns" or "row fields" as scorphus wrote.
so:
Loop through the result. Every iteration handles one row returned from mysql.
*********************************
Here's another way to get the data you want in an array so that you can do the foreach as you tried the first time:
Code: Select all
<?php
$result = mysql_query("SELECT CERTS FROM certs t1 left join faccerts t2 on t1.ID=t2.CID WHERE t2.FID='$fid'");
if ($result !== FALSE){
while ($arrRow = mysql_fetch_array($result )){
$allcerts[] = $arrRow ;
}
}
// Now it is the way you wanted so here comes the rest of your code:
foreach ($allcerts as $eachcert) {
echo ('<p align="center">'.$eachcert.' <br>');
}
?>
Is it easier to grasp now?
