我知道之前已经问过这个问题,但是我希望有人可以帮助我解决我的特定情况(早上5:30,明天就要到了。我在我的最后一根绳子上这里)。这是我的代码:
error_reporting(E_ALL); ini_set('display_errors', 1);
$db = new mysqli('localhost', 'Brendan', 'password', 'Library');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$title = $_GET["title"];
$sql = "SELECT * FROM `BOOK` WHERE `TITLE` like " . $title . " JOIN MANAGES
ON BOOK.SERIAL_NUM = MANAGES.SERIAL_NUMBER JOIN LIBRARIAN
ON MANAGES.ID_NUMBER = LIBRARIAN.ID_NUMBER";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo $row['MANAGES.ID_NUMBER'] . "<br>";
}
当我运行它时,我明白了:
运行查询时出错[您的SQL中有错误 句法;查看与MySQL服务器版本对应的手册 正确的语法在BOOK.SERIAL_NUM =附近使用JOIN MANAGES MANAGES.SERIAL_NUMBER&#39;在第1行]
答案 0 :(得分:3)
尝试
$sql = "SELECT * FROM `BOOK` JOIN MANAGES
ON BOOK.SERIAL_NUM = MANAGES.SERIAL_NUMBER JOIN LIBRARIAN
ON MANAGES.ID_NUMBER = LIBRARIAN.ID_NUMBER
WHERE `BOOK`.`TITLE` like '" . $title . "'";
SQL中的 WHERE
应该在JOIN
语句之后。
答案 1 :(得分:2)
首先,它是正确的,在连接之后必须到达的地方。 另外,请务必确保您的应用程序免受sql injections的攻击,并使用预准备语句绑定参数:
$sql = 'SELECT * FROM `BOOK` JOIN MANAGES
ON BOOK.SERIAL_NUM = MANAGES.SERIAL_NUMBER JOIN LIBRARIAN
ON MANAGES.ID_NUMBER = LIBRARIAN.ID_NUMBER
WHERE `BOOK`.`TITLE` like ?';
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $title);
$stmt->execute();
$result = $stmt->get_result();
在您的情况下,您直接包含由用户定义的$title
,或者可以由用户在您的SQL查询中定义。{/ p>
答案 2 :(得分:1)
MySQL的基本连接格式为:
select ... from A
join B1 on ...
join B2 on ...
...
where ...
所以,在join语句之后放置你的位置,然后再试一次。