lovephp Posted March 20, 2016 Share Posted March 20, 2016 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/301052-sqlstate42000-syntax-error-or-access-violation-1064/ Share on other sites More sharing options...
Jacques1 Posted March 20, 2016 Share Posted March 20, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301052-sqlstate42000-syntax-error-or-access-violation-1064/#findComment-1532235 Share on other sites More sharing options...
lovephp Posted March 20, 2016 Author Share Posted March 20, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301052-sqlstate42000-syntax-error-or-access-violation-1064/#findComment-1532240 Share on other sites More sharing options...
Jacques1 Posted March 20, 2016 Share Posted March 20, 2016 (edited) 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 March 20, 2016 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/301052-sqlstate42000-syntax-error-or-access-violation-1064/#findComment-1532243 Share on other sites More sharing options...
lovephp Posted March 20, 2016 Author Share Posted March 20, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301052-sqlstate42000-syntax-error-or-access-violation-1064/#findComment-1532248 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.