尝试从我的mysql数据库中选择一些特定的数组,我使用了这个:
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` =< '$p2' ORDER BY id DESC") or die(mysql_error());
$ p1和$ p2是5的倍数。例如:0和5,5和10,10和15; 但是使用这段代码,我遇到了这个错误,不知道如何修复它!
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=< '15' ORDER BY id DESC' at line 1
感谢您指导我。
答案 0 :(得分:0)
尝试:
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` <= '$p2' ORDER BY id DESC") or die(mysql_error());
运算符=<
在SQL中不存在。
此外,您可以在此处查看所有比较运算符的列表:http://docs.oracle.com/html/A95915_01/sqopr.htm#i1004774
答案 1 :(得分:0)
就像马克·贝克(Mark Baker)在他给你的评论中所说,它是PHP中的无效运算符
更改此
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` =< '$p2' ORDER BY id DESC") or die(mysql_error());
到此
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` <= '$p2' ORDER BY id DESC") or die(mysql_error());
答案 2 :(得分:0)
用这个替换你的查询..
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` <= '$p2' ORDER BY id DESC") or die(mysql_error());
答案 3 :(得分:0)
试试这个
$result2 = mysql_query("SELECT * FROM books WHERE id >= '$p1' AND id <= '$p2' ORDER BY id DESC") or die(mysql_error());
在PHP代码中不要使用`
=<
应定义为<=
。
改进
使用SELECT *
而不提供所有数据库字段
如果我将您的代码复制到我的IDE,它会显示所有语法错误
答案 4 :(得分:0)
小于或等于运算符仍然存在问题。它应该是(&lt; =)。所以你的查询将是:
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` >= '$p1' AND `id` <= '$p2' ORDER BY id DESC") or die(mysql_error());
或者,您可以使用 BETWEEN 进行范围选择:
$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE `id` BETWEEN '$p1' AND '$p2' ORDER BY id DESC") or die(mysql_error());
答案 5 :(得分:0)
正如其他人所说的那样=&lt;运算符不正确,这可能是错误的原因,但使用'之间'可以简化小于/大于的搜索
$sql="select
`id`, `title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status`
from `books`
where `id` between $p1 and $p2
order by id desc;";
$result2 = mysql_query( $sql ) or die( mysql_error() );