Jump to content

Archived

This topic is now archived and is closed to further replies.

ghop2003

Debugging Help

Recommended Posts

I'm having trouble getting my php code working. I don't really know php all that well. I mainly cut and paste what others have done. This is the error message I get:

Query failed: 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 '20ORDER BY title' at line 1

Here is the part of the code that I think is relevant (if you need more code, let me know):

[code]// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

// Performing SQL query
$cat = $_GET['cat'];
$val = $_GET['val'];
$sort = $_GET['sort'];
if ($sort==NULL)
  $sort = 'title';

$query  = "SELECT title, year, platform, genre, owner, review_url FROM games LIMIT $offset, $rowsPerPage";

if ($cat==NULL or $val==NULL)
  $query .= 'ORDER BY ';
else
  $query .= 'WHERE '.$cat.' LIKE \''.$val.'\' ORDER BY ';

$query .= $sort;

$result = mysql_query($query) or die('Query failed: ' . mysql_error());[/code]

I'm pretty sure it has to do with the first query and second query calls, but I don't know how to fix it. Any help would be appreciated.

Share this post


Link to post
Share on other sites
first off, when concactinating strings, make sure it gets properly spaced. Look at your error: 20ORDER BY title. You can see there that there is no space between 20 and ORDER. add a space in this line (notice the space between the ' and the O)

$query .= ' ORDER BY ';

and you are going to get the same error in your else condition as well. you need to do
' WHERE' not 'WHERE'

Share this post


Link to post
Share on other sites
[!--quoteo(post=385389:date=Jun 18 2006, 03:28 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 18 2006, 03:28 PM) [snapback]385389[/snapback][/div][div class=\'quotemain\'][!--quotec--]
first off, when concactinating strings, make sure it gets properly spaced. Look at your error: 20ORDER BY title. You can see there that there is no space between 20 and ORDER. add a space in this line (notice the space between the ' and the O)

$query .= ' ORDER BY ';

and you are going to get the same error in your else condition as well. you need to do
' WHERE' not 'WHERE'
[/quote]

Thanks for the quick reply. That kind of worked. Now I get this error message:

Query failed: 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 'ORDER BY title' at line 1

I have no idea what to do with that part. I'm thinking it deals with the same section of my code but if it would help, I can include my whole code.

Share this post


Link to post
Share on other sites
I figured it out. I thought I'd post in case anyone else has a similar problem. This section of the code:

[code]$query  = 'SELECT title, year, platform, genre, owner, review_url FROM games LIMIT $offset, $rowsPerPage';

if ($cat==NULL or $val==NULL)
  $query .= ' ORDER BY ';
else
  $query .= ' WHERE '.$cat.' LIKE \''.$val.'\' ORDER BY ';

$query .= $sort;

$result = mysql_query($query) or die('Query failed: ' . mysql_error());[/code]

needs to be changed to this:

[code]$query  = "SELECT title, year, platform, genre, owner, review_url FROM games ";

if ($cat==NULL or $val==NULL)
  $query .= " ORDER BY ";
else
  $query .= " WHERE '.$cat.' LIKE \''.$val.'\' ORDER BY ";

$query .= $sort;

$pagingQuery = " LIMIT $offset, $rowsPerPage ";

$result = mysql_query($query . $pagingQuery) or die('Query failed: ' . mysql_error());[/code]

Like I said before, I don't really know PHP all that well, but I'm guessing the problem had something to do with the ordering of the query (as well as the syntax). I saw this in an example for different php code and gave it a try. It worked.

Share this post


Link to post
Share on other sites

×

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.