-3

如何修复此错误?

您的 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(); 
4

1 回答 1

2

阅读作为整个 mysql 语法错误消息的一部分的标题,您在LIMIT子句上传递的值小于0.

LIMIT 0~, 1~

问题是在这里引起的:

$from = (($page * $max_results) - $max_results)

尝试这个:

$partVal = (($page * $max_results) - $max_results);
$from = ($partVal <= 0 ? 0 : $partVal)

作为旁注,SQL Injection如果变量的值(s)来自外部,则查询很容易受到攻击。请看下面的文章,了解如何预防。通过使用PreparedStatements,您可以摆脱在值周围使用单引号。

于 2013-03-30T10:06:03.143 回答