繁体   English   中英

函数调用返回MySQL语法错误1064

[英]Function call is returning MySQL Syntax Error 1064

每当我调用查询数据库的函数时,MySQL都会一直返回错误1064,但似乎找不到语法错误。

错误消息是

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语句上。Go​​ogle建议我可能由于使用保留关键字而出现错误,但是我没有,也没有插入无效的数据类型。

我已经坚持了很长时间,希望能得到您的投入。谢谢!

limit子句要求两个参数均为非负数 感谢@JoniSalmi提醒您再次查看您的limit子句。

Select Syntax摘录:

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);

限制似乎在第一部分得到负值,至少您的$ 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;

我将三元运算符与isset()一起使用,以确保如果未设置$_GET['page']则该页面默认为1。(即,在第一次加载页面时)

$page = isset($_GET['page']) ? $_GET['page'] : '1';

说明

如果设置了$_GET['page'] $page = $_GET['page']

如果未设置$_GET['page']$page =1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM