I'm trying to show images with a specific subject on the screen. I have 5 images with the same subject and it only shows one image. If I change the subject of the image in the database, the next image with the subject I try to call appears.
function selectSubject($subject){
$showSubject = mysql_query("SELECT name FROM images WHERE subject = '$subject'");
while($showSubject = mysql_fetch_array($showSubject)){
$source_subject_trees = $showSubject['name'];
echo "<img class=\"subject_images\" src='img/$source_subject_bomen'></>";
}
};
mysql_query()
returns a result handle. You then stomp all over that handle by re-assigning to it within your while() loop:
$showSubject = mysql_query(...);
while($showSubject = msyql_fetch_array($showSubject)) {
^^^^^^^^^^^^---here
mysql_fetch_array()
returns an array of one row's data. Since you're assigning to the SAME variable as you stored the actual query result handle, you destroy the result handle... and end up being unable to fetch any more data, because the handle's gone.
mysql_query
you can use GROUP BY subject
to get only distinct resultmysql_query
you can use DISTINCT(name)
to get distinct result on selection timeAny of aboue mention option to get as per your valid result.