错误消息是
Syntax error or access violation: 1064 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 '-10,10' at line 1' in C:\\Users\\Admin\\Downloads\\UniServerZ\\www\\test\\User\\pdofunctions.inc:148\nStack trace:\n#0 C:\\Users\\Admin\\Downloads\\UniServerZ\\www\\test\\User\\pdofunctions.inc(148): PDOStatement->execute()\n#1 C:\\Users\\Admin\\Downloads\\UniServerZ\\www\\test\\User\\search.php(102): GetItemSubCategory(Object(PDO), 0, 0, 'Computers', 'Laptops')\n#2 {main}\n thrown in C:\\Users\\Admin\\Downloads\\UniServerZ\\www\\test\\User\\pdofunctions.inc on line 148, referer: http://localhost/test/User/home.php
我是现实主义者所说的部分
在第1行'-10,10'附近.......
可能很重要,但我似乎无法弄清楚错误。
我的功能:
function GetItemSubCategory($connection,$num,$page,$category,$subcategory)
{
//Results per page
$pagerows=10;
//Tells us the last page number
$last=ceil($num/$pagerows);
// This sets the range of rows to query for the chosen $page
$limit = 'LIMIT ' .($page - 1) * $pagerows .',' .$pagerows;
//Grabbing one page worth of results due to the use of $limit, results are ordered by BusinessName in descending order.
$getpageresult=$connection->prepare("SELECT `ItemID`,`ItemName`,`ItemDesc`,`ItemPrice`,`ItemDP`,`EditDate`,`ItemCategory`,`ItemSubCategory` FROM `Items` WHERE `ItemCategory` =:itemcat AND `ItemSubCategory`=:itemsubcat ORDER BY `EditDate` DESC $limit");
$getpageresult->bindValue(":itemcat",$category);
$getpageresult->bindValue(":itemsubcat",$subcategory);
$getpageresult->execute();
//Initialises the $result variable to display the results of the query
$result = '';
while($row = $getpageresult->fetch(PDO::FETCH_ASSOC)){
$itemid = $row["ItemID"];
$itemname = $row["ItemName"];
$itemdesc=$row["ItemDesc"];
$itemprice=$row["ItemPrice"];
$itemdp=$row["ItemDP"];
$editdate=$row['EditDate'];
$itemcategory=$row["ItemCategory"];
$result .= "<div id='SearchResult' class='SearchResult' >
<div class='SearchResultTop'><span class='SearchResultName'><a href='#'>$itemname</a></span><span class='SearchResultPrice'>$ $itemprice</span></div>
<div class='SearchResultMid'><a href='#' class='SearchResultImg'><img height=130px width=130px src=\"$itemdp\" alt=\"$itemname\"></a><div class='SearchResultDesc'>$itemdesc</div></div>
<div class='SearchResultBtm'><span class='SearchResultEditDate'>$editdate</span></div>
</div>";
}
return $result;
}
我的HTML(摘录):
//Additional code above which sets up the view,cat and subcat variable
elseif($view =="items")
{
//Grabs total number of items and assigns it to the $count variable
$count=GetItemCountSubCategory($cxn,$cat,$subcat);
//Sets number of results on a page
$pagerows=10;
//Tells us the last page number
$last=ceil($count/$pagerows);
//Checks if the $_GET variable is present and sets the page to 1 if it is not
if(!isset($_GET['page']))
{
$pagenum = 1;
}
else
{
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1)
{
$pagenum = 1;
}
else if ($pagenum > $last)
{
$pagenum = $last;
}
$pagecounter = "Page <b>$pagenum</b> of <b>$last</b>";
$result=GetItemSubCategory($cxn,$count,$pagenum,$cat,$subcat);
$pagination=Pagination($cxn,$count,$pagenum,$view);
}
我知道,因为错误消息指向包含execute()的行;问题可能在于SELECT语句。谷歌建议我可能因使用保留关键字而得到错误,但我不是,也不是我插入无效数据类型。
我已经坚持了很长一段时间并且会感激任何输入。谢谢!
答案 0 :(得分:2)
limit
子句要求两个参数都是非负。感谢@JoniSalmi提醒您再次查看限制条款。
LIMIT子句可用于约束返回的行数 通过SELECT语句。 LIMIT需要一个或两个数字参数, 必须都是非负整数常量(使用时除外) 准备好的陈述)。
因此,为了使用限制,您需要更新查询以使用参数化限制:
//Results per page
$pagerows=10;
//Tells us the last page number
$last=ceil($num/$pagerows);
// This sets the range of rows to query for the chosen $page
$limit = ($page - 1) * $pagerows;
$getpageresult=$connection->prepare("SELECT `ItemID`,`ItemName`,`ItemDesc`,`ItemPrice`,`ItemDP`,`EditDate`,`ItemCategory`,`ItemSubCategory` FROM `Items` WHERE `ItemCategory` =:itemcat AND `ItemSubCategory`=:itemsubcat ORDER BY `EditDate` DESC limit :limit, :offset");
$getpageresult->bindValue(":itemcat",$category);
$getpageresult->bindValue(":itemsubcat",$subcategory);
$getpageresult->bindValue(":limit", $limit);
$getpageresult->bindValue(":offset", $pagerows);
答案 1 :(得分:1)
限制似乎第一部分的负值至少是你的$ page
$limit = 'LIMIT ' .($page - 1) * $pagerows .',' .$pagerows;
试试这个
更改强>
if ($pagenum < 1)
{
$pagenum = 1;
}
else if ($pagenum > $last)
{
$pagenum = $last;
}
要强>
if ($pagenum > $last)
{
$pagenum = $last;
}
if ($pagenum < 1)
{
$pagenum = 1;
}
似乎$ last只能覆盖导致您出现问题的$pagenum=0
。
也许这应该是
//change
$last=ceil($count/$pagerows);
//to
$last=$count * $pagerows;
答案 2 :(得分:0)
我使用ternary运算符和isset()
来确保如果未设置$_GET['page']
,页面默认为1(即首次加载页面时)
$page = isset($_GET['page']) ? $_GET['page'] : '1';
解释
如果$_GET['page']
设置为$page = $_GET['page']
如果$_GET['page']
未设置$page =1