Error in MySQL Syntax error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah ok, then have this sql statment printed

Code: Select all

$q = 'SELECT * FROM comments ORDER BY Date DESC LIMIT '.$from.', '.$max_results.'';
echo "And the query is: '$q'. ";
$r = mysql_query($q) or die('There was an error in the query at line ' . __LINE__ . ' of the script: ' . mysql_error());
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

feyd wrote:Did you place that line before the call to mysql_query()?
Yeah, I placed right above the mysql queries. I tried posting it down below too but that didnt work.
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

volka wrote:ah ok, then have this sql statment printed

Code: Select all

$q = 'SELECT * FROM comments ORDER BY Date DESC LIMIT '.$from.', '.$max_results.'';
echo "And the query is: '$q'. ";
$r = mysql_query($q) or die('There was an error in the query at line ' . __LINE__ . ' of the script: ' . mysql_error());
Lol, I tried but it wont.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:?:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

did you try my suggestion? (last post of page one)
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

Using your suggestion added a bit more info..
There was an error in the query at line 57 of the script: 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 '' at line 1 Query: SELECT * FROM comments ORDER BY Date DESC LIMIT 0,
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

bingo..

now you can see that there is a value missing.
Last edited by Jenk on Mon Aug 14, 2006 9:04 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

SELECT * FROM comments ORDER BY Date DESC LIMIT '.$from.', '.$max_results
SELECT * FROM comments ORDER BY Date DESC LIMIT 0,
Tells you what?
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

That I need to get a new pagination system? Thats what the variables are in there for.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do this...

Code: Select all

<?php
echo '<p>From is ' . $from . ' and max_results is ' . $max_results . '...</p>';
$q = 'SELECT * FROM comments ORDER BY Date DESC LIMIT '.$from.', '.$max_results.'';
echo '<p>And the query is: ' . $q . '</p>';
if ( !$r = mysql_query($q) )
{
    die('There was an error in the query at line ' . __LINE__ . ' of the script: ' . mysql_error());
}
else
{
    echo '<p>The query finally succeeded...</p>';
}
?>
I suspect it has something to do with $from and $max_results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Man, you guys are posting faster than I can think of what's for dinner. That is crazy (yet awesome and humbling at the same time).
Last edited by RobertGonzalez on Mon Aug 14, 2006 9:11 pm, edited 1 time in total.
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

That code returns...
From is 0 and max_results is ...

And the query is: SELECT * FROM comments ORDER BY Date DESC LIMIT 0,
There was an error in the query at line 61 of the script: 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 '' at line 1
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, now its time to post the code where $max_result gets its value. That will fix your query.
adamb10
Forum Commoner
Posts: 91
Joined: Sat Jun 24, 2006 7:44 pm

Post by adamb10 »

Code: Select all

$max_results = $settings['maxshow'];
$from = ($page * $max_results);
The value is retrived from these lines of code...

Code: Select all

$result = mysql_query("SELECT * FROM settings") or die('There was an error in the query at line ' . __LINE__ . ' of the script: ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
    $settings[$row['name']] = $row['value'];
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ok, so $settings['maxshow'] either doesn't exist or is an empty string (or similar.)

You need to trace back where it's being set until you find why it isn't being set (correctly.)
Post Reply