0

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'></>";
        }
    };
1
  • 2
    You're using the same variable for both the result of the query, and for the return value of mysql_fetch_array. Rename one of them, and you should be fine - at the moment, the database result set in $showSubject is being over-written by the first record, and ending the loop. Commented Oct 21, 2013 at 17:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. In mysql_query you can use GROUP BY subject to get only distinct result
  2. In mysql_query you can use DISTINCT(name) to get distinct result on selection time

Any of aboue mention option to get as per your valid result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.