0

in this code I am trying to output all images with the tag I searched. I created 2 images with the same tag 'hi'.

The program is only outputting one of the images though. But when I echo the $rows number it equals 2. When I ran the query in MYSQL it also returned 2 rows. I do not see why ALL the images are being shown.

 $input = ($_GET['input']);
$query = "SELECT * FROM `photo`.`photo` WHERE `tags` LIKE '%$input%'";
$result = mysql_query($query);
$data = mysql_fetch_array($result) or die (mysql_error());
$rows = mysql_num_rows($result);
$image = $data['image'];
     header('Content-type: image/jpeg');
     echo $image;

This isn't all my code but it is all that I think is necessary. Here is what is being shown in another page.

echo "<img src='photolarge.php?input=$input'>"

When I echo $rows it outputs the number 2 so I know the query is working.

8
  • 1
    ^^^^as true as it is, its getting a little tedious for regular S.O users to see it 10+ times a day Commented Feb 7, 2013 at 23:24
  • I'm doing it for Buddha. Commented Feb 7, 2013 at 23:26
  • regardless of house many times its seen, it still needs to be said :/ Commented Feb 7, 2013 at 23:27
  • 1
    Oh come on "don't use mysql_" doesn't need posting on every question that mentions it. Let's be realistic, until every copy of the php mysql extension is deleted from all servers in all of the world, it will still work. Obsessive is an understatement. Commented Feb 7, 2013 at 23:34
  • @popnoodles You're free to not encourage best practice. Commented Feb 7, 2013 at 23:47

3 Answers 3

1

You need to use a loop to iterate through the result array.

$query = "SELECT * FROM `photo`.`photo` WHERE `tags` LIKE '%$input%'";
$result = mysql_query($query);
while($rows = mysql_fetch_array($result)) {
    echo $rows[image];
}

As I mentioned in the comments, please don't use mysql_ functions. You also need to sanitize your parameter as your code is vulnerable to SQL injection.

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

3 Comments

Ok well I know what you are doing but I am also re-sizing the image in the same file. so I tried codewhile($data = mysql_fetch_array($result)) { $image = $data['image']; code And that still only would output 1 image
I need a variable so I can re-size it.
Ok I tried what you did and it still did not work ob_start(); $input = $_GET['input']; $query = "SELECT * FROM photo.photo` WHERE tags LIKE '%$input%'"; $result = mysql_query($query); while($rows = mysql_fetch_array($result)) { header('Content-type: image/jpeg'); echo $rows['image'];`
0

You need to fetch all returned rows in line 4, not only the first one.

Comments

0

try this

   $query = "SELECT * FROM `photo`.`photo` WHERE `tags` LIKE '%$input%'";
   $result = mysql_query($query);
   while($row = mysql_fetch_array($result)) {
   echo $row['image'];
   }

tips: try to change to MYSQLI or PDO ,MYSQL is no longer maintained

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.