Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在每个图像下显示注释_Php_Sql - Fatal编程技术网

Php 在每个图像下显示注释

Php 在每个图像下显示注释,php,sql,Php,Sql,我想显示数据库中每张图片下发布的所有评论。 我尝试了以下代码,但我只能得到一条评论,这是最后一条 $sql1="SELECT user,comment FROM comment_table where imagename=:file"; $q1=array(':file'=>$file); try { $stmt = $pdo->prepare($sql1); $stmt->execute($q1); $stmt->setFetchMode(PDO::FE

我想显示数据库中每张图片下发布的所有评论。
我尝试了以下代码,但我只能得到一条评论,这是最后一条

$sql1="SELECT user,comment FROM comment_table where imagename=:file";
$q1=array(':file'=>$file);
try {
  $stmt   = $pdo->prepare($sql1);
  $stmt->execute($q1);
  $stmt->setFetchMode(PDO::FETCH_BOTH);

  $result= $stmt->fetch();
  $c = $result["comment"];
  $u=$result["user"];

}
catch (PDOException $e) {
  die("Failed to run query: " . $e->getMessage());
}
echo "<tr><td>".$u.":".$c."</td><tr>";
$sql1=“从注释表中选择用户,注释,其中imagename=:file”;
$q1=数组(':file'=>$file);
试一试{
$stmt=$pdo->prepare($sql1);
$stmt->execute($q1);
$stmt->setFetchMode(PDO::FETCH_两者);
$result=$stmt->fetch();
$c=$result[“comment”];
$u=$result[“用户”];
}
捕获(PDO$e){
die(“无法运行查询:”..e->getMessage());
}
回声“$u.”:“$c.”;

您需要并感谢您的帮助。

您需要为每个人创建一个
foreach
来打印所有评论。在这里读一读

对于您的代码,它只显示一条注释,因此请尝试以下操作:

$sql1="SELECT user,comment FROM comment_table where imagename=:file";
                            $q1=array(':file'=>$file);
                            try {
    $stmt   = $pdo->prepare($sql1);
    $stmt->execute($q1);
    $stmt->setFetchMode(PDO::FETCH_BOTH);

$result= $stmt->fetch();

catch (PDOException $e) {
    die("Failed to run query: " . $e->getMessage());
}

foreach ($result as $res) {
    echo "<tr><td>".$res["user"].":".$res["comment"]."</td><tr>";
}
$sql1=“从注释表中选择用户,注释,其中imagename=:file”;
$q1=数组(':file'=>$file);
试一试{
$stmt=$pdo->prepare($sql1);
$stmt->execute($q1);
$stmt->setFetchMode(PDO::FETCH_两者);
$result=$stmt->fetch();
捕获(PDO$e){
die(“无法运行查询:”..e->getMessage());
}
foreach(结果为$res){
回显“$res[“用户”]”:“$res[“注释”]”;
}
您还可以执行以下操作:while($comment=$stmt->fetch()){echo$comment['comment']}