Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 10-20-2006, 03:38 PM SQL Syntax Error
Junior Talker

Posts: 3
Name: Rich
Trades: 0
After moving my server to a new location I am now receiving a SQL syntax error on some of my pages. Does this have to do with a change in SQL versions?

The error reads:
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


This error occurs in links such as:
http://www.partybody.com/viewUserProfile.php?userId=8803
&
http://www.partybody.com/nightlife/parties?partyid=222

But not in a links such as:
http://www.partybody.com/viewUserProfile.php?userId=50
&
http://www.partybody.com/nightlife/parties?partyid=59
partybody is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-20-2006, 04:18 PM Re: SQL Syntax Error
pitbull82's Avatar
Super Talker

Posts: 147
Name: Marcin Nabiałek
Location: Poland, Częstochowa
Trades: 0
I suppose there is some problem with LIMIT fraze, you have probably LIMIT(-10,10) in your syntax, whereas

Quote:
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 non-negative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
Your application probably calculates something wrong for id so negative numbers appear and cause error syntax
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
pitbull82 is offline
Reply With Quote
View Public Profile Visit pitbull82's homepage!
 
Old 10-20-2006, 04:57 PM Re: SQL Syntax Error
Junior Talker

Posts: 3
Name: Rich
Trades: 0
Thanks for the insite. I will look into that.

What I'm not understanding is that the problem appears on more than one php page, however it doesn't occur with every page that responds to the same code. Which leads me to believe that it's a databse issue and not a php issue.

Everything was working fine before also. Now that I have switched SQL versions the error is occuring.
partybody is offline
Reply With Quote
View Public Profile
 
Old 10-20-2006, 05:38 PM Re: SQL Syntax Error
Junior Talker

Posts: 3
Name: Rich
Trades: 0
I think I figured it out. Here is the code that is displaying total comments as -10 when it is 0. This is causing any member without a comment to get a syntax error. Does anyone know how to fix that?

$commentpage = $_REQUEST["commentpage"];
if ($commentpage == "") $commentpage = 1;
$rs = $db->prepare("SELECT COUNT(*) FROM tblusercomment WHERE posted_id = ?");
$rs->execute($userid);
$row = $rs->fetch_array();
$totalcomments = $row[0];
$perpage = 10;
$totalpages = ceil($totalcomments / $perpage);
if ($commentpage == "all") {
$sql = "SELECT tblusercomment.*, tblmember.* FROM tblusercomment " .
"INNER JOIN tblmember ON tblusercomment.poster_id = tblmember.user_id " .
"WHERE posted_id= ? " .
"ORDER BY tblusercomment.timestamp DESC";
$first = 1;
if ($totalpages > 5) {
$last = 5;
} else {
$last = $totalpages;
}
$commentpage = 0;
} else {
if ($commentpage < 1) $commentpage = 1;
if ($commentpage > $totalpages) $commentpage = $totalpages;

if ($totalpages > 5) {
if ($commentpage == 1) {
$first = 1;
$last = 5;
} elseif ($commentpage <= 2) {
$first = 1;
$last = $commentpage + 3;
} else {
$first = $commentpage - 2;
$last = $commentpage + 2;
}
} else {
$first = $commentpage;
$last = $totalpages;
}
if ($totalpages < $last) $last = $totalpages;

$sql = "SELECT tblusercomment.*, tblmember.* FROM tblusercomment " .
"INNER JOIN tblmember ON tblusercomment.poster_id = tblmember.user_id " .
"WHERE posted_id= ? " .
"ORDER BY tblusercomment.timestamp DESC LIMIT " . ( ($commentpage - 1) * $perpage ) . ", $perpage";
}
if ($commentpage > 1) {
$data = array("page" => "<a href=/viewUserProfile.php?userId=$userid><<</a>");
$pm->addLoop("comment-pages", $data);
$data = array("page" => "<a href=/viewUserProfile.php?userId=$userid&commentpage=" . ($commentpage - 1) . "><</a>");
$pm->addLoop("comment-pages", $data);
}
for ($i = $first; $i <= $last; $i++) {
if ($i != $commentpage) {
$data = array("page" => "<a href=/viewUserProfile.php?userId=$userid&commentpage=$i> $i</a>");
} else {
$data = array("page" => $i);
}
$pm->addLoop("comment-pages", $data);
}
$firstcomment = (($commentpage - 1) * $perpage) + 1;
$lastcomment = ($commentpage * $perpage);
if ($lastcomment > $totalcomments) $lastcomment = $totalcomments;
$pm->setVariable("firstcomment", $firstcomment);
$pm->setVariable("lastcomment", $lastcomment);
$pm->setVariable("totalcomments", $totalcomments);
if ($commentpage < $totalpages) {
$data = array("page" => "<a href=/viewUserProfile.php?userId=$userid&commentpage=" . ($commentpage + 1) . ">></a>");
$pm->addLoop("comment-pages", $data);
$data = array("page" => "<a href=/viewUserProfile.php?userId=$userid&commentpage=$to talpages>>></a>");
$pm->addLoop("comment-pages", $data);
}
$rs = $db->prepare($sql);
$rs->execute($userid);
while ($row = $rs->fetch_array()) {
// TODO: This stripslash is a stupid hack
$row["comment"] = stripslashes($row["comment"]);
$row["date"] = date("m/d/Y h:i a", $row["timestamp"]);
if ($userid == $_SESSION["userinfo"]["userid"]) {
$row["comment-control"] = "<form style=\"margin: 0px; padding: 0px; display: inline;\" action=\"viewUserProfile.php\" method=\"post\">" .
"<input type=\"hidden\" name=\"userId\" value=\"$userid\">" .
"<input type=\"hidden\" name=\"commentId\" value=\"" . $row["id"] . "\">" .
"<input type=\"submit\" name=\"btn\" value=\"Delete\"></form>";
} elseif ($row["poster_id"] == $_SESSION["userinfo"]["userid"]) {
$row["comment-control"] = "<form style=\"margin: 0px; padding: 0px; display: inline;\" action=\"editComment.php\" method=\"post\">" .
"<input type=\"hidden\" name=\"commentId\" value=\"" . $row["id"] . "\">" .
"<input type=\"submit\" name=\"btn\" value=\"Edit\"></form>";
}
$pm->addLoop("usercomments", $row);
}
partybody is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL Syntax Error
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34220 seconds with 12 queries