简体   繁体   中英

Function call is returning MySQL Syntax Error 1064

MySQL keeps returning an error 1064 whenever i call a function to query my database, yet i cant seem to find my syntax error.

The error message is

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

i realist that the part where it states

near '-10,10' at line 1' .....

is probably important but i cant seem to figure out the error.

My Function:

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

My HTML(extract):

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

}

I know that since the error message points to the line containing execute(); the problem should probably lie with the SELECT statement.Google has suggested that i may be getting the error due to the usage of reserved keywords, but i'm not, nor am i inserting invalid data types.

I've been stuck on this for quite some time and would appreciate any input.Thanks!

limit clause requires both arguments to be non-negative . Thanks to @JoniSalmi for reminding to take a second look at your limit clause.

Excerpt from the Select Syntax :

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

So in order to use the limits as is you need to update your query to use parameterized 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);

Limit seems to get negative values for the first part at least what is your $page

$limit = 'LIMIT ' .($page - 1) * $pagerows .',' .$pagerows;

Try this

Change

if ($pagenum < 1) 
{ 
    $pagenum = 1; 
}
else if ($pagenum > $last) 
{ 
    $pagenum = $last; 
}

To

if ($pagenum > $last) 
{ 
    $pagenum = $last; 
}
if ($pagenum < 1) 
{ 
    $pagenum = 1; 
}

It seems like $last is only thing that could override your $pagenum=0 which would cause your problem.

Maybe this should be

//change
$last=ceil($count/$pagerows);
//to
$last=$count * $pagerows;

I use the ternary operator along with isset() to ensure that the page defaults to 1 if $_GET['page'] is not set.(ie at first load of page)

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

Explanation

if $_GET['page'] is set $page = $_GET['page']

if $_GET['page'] is NOT set $page =1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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