Jump to content


Photo

Debugging Help


  • Please log in to reply
3 replies to this topic

#1 ghop2003

ghop2003
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 18 June 2006 - 07:24 PM

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):

// 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());

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.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 18 June 2006 - 07:28 PM

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'
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 ghop2003

ghop2003
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 18 June 2006 - 07:43 PM

[!--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) View Post[/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.

#4 ghop2003

ghop2003
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 June 2006 - 03:09 AM

I figured it out. I thought I'd post in case anyone else has a similar problem. This section of the 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());

needs to be changed to this:

$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());

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users