Jump to content

SQLSTATE[42000]: Syntax error or access violation: 1064


Recommended Posts

if there is no records in my table then im getting this error on the page'

 

 

SQLSTATE[42000]: 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 '-5' at line 1

 

 

how could i fix this anyone?

 

my php par is here

 

 try {
    require_once 'db.php';
    $total = $db->query('SELECT COUNT(*) FROM article')->fetchColumn();
    
    $per_page = 5;
    
    $pages = ceil($total / $per_page);
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    $offset = ($page - 1)  * $per_page;
    $start = $offset + 1;
    $end = min(($offset + $per_page), $total);

   $stmt = $db->prepare('SELECT * FROM article ORDER BY ne_title LIMIT :limit OFFSET :offset');

 
    $stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    
    if ($stmt->rowCount() > 0) {
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        foreach ($iterator as $row) {
                $desc = $row["ne_article"];
                $img = $row['ne_image'];
                if(!empty($img)){
                $img = '<br/><img src="uploads/images/'.$img.'" alt="" class="responsive-shrink">';
                }else{
                $img = '';
                }
                $youtube = $row["ne_youtube"];    
                if(!empty($youtube)){
                $youtube = '<br/><div class="video-container"><iframe src="http://www.youtube.com/embed/'.$youtube.'"></iframe></div>';
                }else{
                $youtube = '';
                }
        echo '<h4><a href="'.$row['ne_url'].'">'.$row['ne_title'].'</a></h4>
                <h5><b>Views:</b> '.$row['views'].', <b>Posted on:</b> '.format_date($row['created']).'</h5>                
                '.$img.'
                '.$youtube.'
                <p>
                <br/>'.bbcode(nl2br(shortenString($desc))).'
                </p>
                 <div id="pagelink">
                <a href="'.$row['ne_url'].'" class="myButton"> Read more...</button></a>
                    </div>';        
        }
    echo '<div id="pagination">
    <div id="pagiCount">';
        $prevlink = ($page > 1) ? '<span id="prev"><a href="?page=1" title="First page">First</a></span> <span id="prev"><a href="?page=' . ($page - 1) . '" title="Previous page"><<</a></span>' : '';
        $nextlink = ($page < $pages) ? '<span id="next"><a href="?page=' . ($page + 1) . '" title="Next page">>></a></span> <span id="next"><a href="?page=' . $pages . '" title="Last page">Last</a></span>' : '';
        echo '<div id="paging"><p><small>', $prevlink, ' Page ', $page, ' of ', $pages, '', $nextlink, '</small></p></div>';        
    echo '</div>    
    </div>';
    } else {
        echo '<p>No resilts found.</p>';
    }
} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}

First off: Get rid of this try-catch nonsense. Then you'll see a proper error message with the actual location of the problem, and your production server won't clutter your site with PHP errors.

 

Appearently your script has calculated a negative offset (-5). You can't have that in MySQL. You get a negative offset when, for example, there are no articles. The script isn't capable of handling this case.

First off: Get rid of this try-catch nonsense. Then you'll see a proper error message with the actual location of the problem, and your production server won't clutter your site with PHP errors.

 

Appearently your script has calculated a negative offset (-5). You can't have that in MySQL. You get a negative offset when, for example, there are no articles. The script isn't capable of handling this case.

 

help fix plz bro :)

No, bro, I will not fix your code. I told you what's wrong and what to check, the rest is your job.

 

You've been using PHP for at least 2 years now. It's time that you learn to think for yourself and solve your own problems, because there won't always be somebody to spoon-feed you.

Edited by Jacques1
  • Like 1

No, bro, I will not fix your code. I told you what's wrong and what to check, the rest is your job.

 

You've been using PHP for at least 2 years now. It's time that you learn to think for yourself and solve your own problems, because there won't always be somebody to spoon-feed you.

agreed

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.