如何解决此错误?
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“-20,20”附近使用正确的语法
我网站上的相同脚本工作正常,但我上传到另一台Windows服务器并收到此错误。
<?php
//connect to database
mysql_connect('xyz.ipowermysql.com','itomi','password.');
mysql_select_db('itomi');
$max_results = 20;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM `itomi` WHERE `ntitle` LIKE '".$letter."%' ORDER BY `ntitle` ASC LIMIT $from, $max_results";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
echo "<table class='hovertable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>Keyword</th><th>Title</th><th>Detail</th></tr>";
if ($rows > 0) {
while($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo '<a href="detail.php?id=' . $row['id'] . '" class="style1">' .$row['ntitle'].' </a>';
echo "</td><td>";
echo $row['ndetails'];
echo "</td><td>";
echo $row['counter'];
echo "</td></tr>";
}
} else {
echo "<tr><td colspan=\"5\">No results found!</td></tr>";
}
echo "</table>";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as ntitle FROM itomi ORDER BY ntitle ASC"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<p class=\"style1\">Pages: ";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$prev&letter=$letter\" class=\"style1\">Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo " ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['php_SELF']."?page=$next&letter=$letter\" class=\"style1\">Next</a>";
}
echo "</p>";
mysql_close();
答案 0 :(得分:2)
读取作为整个mysql语法错误消息一部分的标题,您在LIMIT
子句上传递的值小于0
的最小值。
LIMIT 0~, 1~
问题在这里引起:
$from = (($page * $max_results) - $max_results)
试试这个:
$partVal = (($page * $max_results) - $max_results);
$from = ($partVal <= 0 ? 0 : $partVal)
作为旁注,如果变量的值( s )来自外部,则查询易受SQL Injection
攻击。请查看下面的文章,了解如何防止它。通过使用PreparedStatements
,您可以摆脱在值周围使用单引号。