我试图为可以搜索和缩小学生搜索范围的网站实施搜索下拉菜单。我的代码中出现以下错误:
Check the documentation for your version for the right syntax to use near lname LIKE '%dff%'' at line 1
这是我的代码:
<?php
ob_start();
#Start Session:
session_start();
#Database Connection
include_once('../config/setup.php');
if(isset($_GET['s']) && $_GET['s'] != ''){
$s = $_GET['s'];
$sql = "SELECT * FROM students WHERE fName, lName LIKE '%$s%'"; <**--- this where the website detects the error, something about the like query is wrong please help!!**
$result = mysqli_query($dbc, $sql);
while($row = mysqli_fetch_array($result,$dbc)){
$url = $row['fName'];
$title = $row['lName'];
echo "<div style='' id='searchtitle'>" . "<a style='font-family: verdana; text-decoration: none; color: black;' href='$url'>" . $title . "</a>" . "</div>";
}
}
?>
答案 0 :(得分:6)
您的where
子句要么缺少参数,要么删除一个参数。这解释了语法错误。
WHERE fName, lName LIKE '%$s%'";
^^^^^^ // it starts there
如果您正在查找fName和lName是否等于您的搜索,则需要执行
WHERE fName LIKE '%$s%' OR lName LIKE '%$s%'";
旁注:如果您希望与OR
匹配,则可以将AND
替换为WHERE fName LIKE '%$s%'";
。
或只有一个:
SELECT field1, field2,...fieldN table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
有关LIKE的更多信息,请访问http://www.mysqltutorial.org/mysql-like/及其语法。
另外,手册https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
这是另一个http://www.tutorialspoint.com/mysql/mysql-like-clause.htm
从^
拉出的示例mysqli_fetch_array($result,$dbc))
,$dbc
从那里删除John's place
。数据库连接仅属于您的查询。此外,如果您的查询包含MySQL可能会抱怨的字符,例如撇号,例如$s = mysqli_real_escape_string($dbc, $_GET['s']);
,那么您需要转义数据:
or die(mysqli_error($dbc))
使用mysqli_query()
到image_tag
会发出语法错误信号。