如何将多个AND与变量放在php / mysql查询中?

时间:2014-10-20 00:30:34

标签: php mysql

尝试了许多不同的东西以使其工作,如果它甚至可以将2个AND的变量放入1个查询中,则无法在互联网上找到答案。希望有人能帮助我解决这个问题。

我试图制作一个1-5分的评分系统。数据库中有一个评级行,值应该放在前面 / 5

首先尝试:

    $query2 = "SELECT rating, gebruikersnaam, Foto.fotoid FROM rating, Foto WHERE rating.fotoid = Foto.fotoid AND Foto.fotoid = '".$id."'" AND gebruikersnaam "'".$gebruikersnaam."'";

编辑版

"SELECT rating, gebruikersnaam, Foto.fotoid
 FROM rating
 INNER JOIN Foto ON rating.fotoid = Foto.fotoid
 WHERE rating.fotoid = Foto.fotoid AND Foto.fotoid = '" . $id . "' AND gebruikersnaam '" . $gebruikersnaam . "'";
 $result2 = mysqli_query($GLOBALS['conn'], $query2);

var_dump($result2);
echo $query2;

$row2 = mysqli_fetch_array($result2);
echo "<h2>".$row2['gebruikersnaam']."</h2>";
echo "<h2>".$row2['rating']."/5</h2>";

的var_dump(RESULT2);呼应:

布尔(假)

query2回声:

SELECT rating,gebruikersnaam,Foto.fotoid FROM rating INNER JOIN Foto ON rating.fotoid = Foto.fotoid WHERE rating.fotoid = Foto.fotoid AND Foto.fotoid ='54'AND gebruikersnaam'Kees'

echo "<h2>".$row2['rating']."/5</h2>";

代码似乎不再获得['评级']了?

1 个答案:

答案 0 :(得分:1)

这应该有效!你在字符串中放了很多""!也可以加入第二个表格。

 $query2 = "SELECT rating, gebruikersnaam, Foto.fotoid
            FROM rating
            INNER JOIN Foto ON rating.fotoid = Foto.fotoid
            WHERE rating.fotoid = Foto.fotoid AND Foto.fotoid = '" . $id . "' AND gebruikersnaam '" . $gebruikersnaam . "'";