php/mysql error

  • DavyDuke17
  • Graduate
  • Graduate
  • DavyDuke17
  • Posts: 166

Post 3+ Months Ago

Alright, I created a database where I can put news for my site. It has 4 fields, the nid which is auto increment, the headline, the body, and teams (baseball teams) to which it pertains. This way on the team pages I can have the latest news (or highest news id, nid). I have everything good so far except when I try to pick the latest news for only that team. Here is the code I am using:
Code: [ Select ]
$sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ";
$sql .= "ORDER BY nid ASC ";

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$variable1=$row["nid"];
$variable2=$row["stitle"];
$variable4=$row["body"];
}
  1. $sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ";
  2. $sql .= "ORDER BY nid ASC ";
  3. $result = mysql_query($sql) or die(mysql_error());
  4. while($row = mysql_fetch_array($result))
  5. {
  6. $variable1=$row["nid"];
  7. $variable2=$row["stitle"];
  8. $variable4=$row["body"];
  9. }

When I try this I get this error message: 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 nid ASC LIMIT 0, 30' at line 1. If I run the sql statement just how it is in phpmyadmin it works good. If I take that line (the order by) out I get query is empy. Help is greatly appreciated. Oh and just so you know...the variables are all used later embedded in html. My thinking is that if its sorted ASC then the last results it does will be the one with the highest NID, therefore the newest one.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

try writing it all as one statement:
$sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ORDER BY nid ASC ";
that command should work, returning any entries that have the letter group "Atl" in the "teams" column. Even rewriting it as:
$sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ORDER BY nid ASC LIMIT 0, 30";
Should return something to you.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 162 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.