gpt4 book ai didi

php - SQL Select 字符串不工作

转载 作者:行者123 更新时间:2023-11-30 22:30:39 25 4
gpt4 key购买 nike

我已经发布了这个,但我想更好地解释一下。我有一个网站,学生在其中插入他们的 4 门类(class)和每门类(class)的 4 条评论,这些是 SQL 表列:类(class) 1、类(class) 2、类(class) 3、类(class) 4、评论 1、评论 2、评论 3、评论 4。

我有一个搜索,任何学生都可以在其中输入地理(可以保存在四个类(class)列中的任何一个),我希望我的 SQL 查询返回所有关于地理的评论。例如,如果学生将地理保存在 Course2 位置,我希望我的 SQL 查询选择 comment2,其中 course2 = geography。他可能已经保存在course1中了,所以要灵活,但是只选择学生选择的类(class)。这是我当前的 SQL 查询:

$SQL = "SELECT (Comment1 FROM Students WHERE Course1 = 'geography'), (Comment2 FROM Students WHERE Course2 = 'geography'), (Comment3 FROM Students WHERE Course3 = 'geography'),  (Comment4 FROM Students WHERE Course4 = 'geography')";

目前,此 SQL 查询无效。我知道这个结构可能看起来很奇怪,但从逻辑上讲,正如您可能理解的那样,这是有道理的,尽管这可能不是正确的编码方式。然后我像这样打印所有地理评论:

$null = '';
if(mysql_num_rows($result)) {
echo "<ol>";
while ($row=mysql_fetch_array($result)) {
if($row["Class"]!=$null) {
if($null!='') {
echo "</ol><ol type='1'>";
}
}

echo "<li><p>" . " " . $row["Comment1"]. " " . $row["Comment2"]." " . $row["Comment3"] . " " . $row["Comment4"] . "</li></p>";
$i = $i +1;
}

echo "</ol>";

最佳答案

您的查询是错误的:试试这个

SELECT Comment1, Comment2, Comment3, Comment4 
FROM Students
WHERE Course1 = '$value1' AND Course2 = '$value1'
AND Course3 = '$value1' AND Course4 = '$value1'

SELECT Comment1, Comment2, Comment3, Comment4 
FROM Students
WHERE Course1 = '$value1' OR Course2 = '$value1'
OR Course3 = '$value1' OR Course4 = '$value1'

根据您的要求,在检查类(class)时使用 AND 或 OR

关于php - SQL Select 字符串不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com