简体   繁体   English

并非所有行都输出PHP Mysql

[英]Not all rows outputting PHP Mysql

in this code I am trying to output all images with the tag I searched. 在这段代码中,我试图输出带有搜索标签的所有图像。 I created 2 images with the same tag 'hi'. 我创建了2个具有相同标签“ 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. 但是,当我回显$ rows数字时,它等于2。当我在MYSQL中运行查询时,它还返回了2行。 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. 当我回显$ rows时,它输出数字2,因此我知道查询正在工作。

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. 正如我在评论中提到的,请不要使用mysql_函数。 You also need to sanitize your parameter as your code is vulnerable to SQL injection. 您还需要清理参数,因为您的代码容易受到SQL注入的攻击。

您需要获取第4行中所有返回的行,而不仅仅是第一行。

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 提示:尝试更改为MYSQLIPDO ,不再维护MYSQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM