thetoolman Posted December 15, 2008 Share Posted December 15, 2008 Hi there, I am getting this error with the following code. 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 dateposted' at line 1 I was told that I need to define $row['id'] in this line: blog_id = " . $row['id'] . " But I am not sure how to do so. Does anyone have any ideas why I am getting that error? This is my code: $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo "<h1>" . $row['subject'] . "</h1>"; echo "<h3>Posted in: <a href='view_category.php?id=" . $row['cat_id'] . "'>" . $row['cat'] . "</a><br /> on " . date("l jS F Y", strtotime($row['dateposted'])) . "at " . date("H:i a", strtotime($row['dateposted'])) . "</h3><br />"; echo nl2br($row['body']); $comments_sql = "SELECT name, comment FROM comments WHERE blog_id = " . $row['id'] . " ORDER BY dateposted;"; $comments_result = mysql_query($comments_sql); while($comments_row = mysql_fetch_array($comments_result)){ echo "<h1>Commented by: " . $comments_row['name'] . "</h1><h3>on " . date("l jS F Y", strtotime($row['dateposted'])) . "at " . date("H:i a", strtotime($row['dateposted'])) . "</h3><br /><h3>" . $comments_row['comment'] . "</h3><div class='dotted_div'></div>"; } Link to comment https://forums.phpfreaks.com/topic/137110-error-with-mysql-code/ Share on other sites More sharing options...
darkfreaks Posted December 15, 2008 Share Posted December 15, 2008 Fix: <?php comments_sql = "SELECT name, comment FROM comments WHERE blog_id = '".$row['id']."' ORDER BY dateposted;"; ?> Link to comment https://forums.phpfreaks.com/topic/137110-error-with-mysql-code/#findComment-716165 Share on other sites More sharing options...
thetoolman Posted December 15, 2008 Author Share Posted December 15, 2008 Thanks. I tried that but the error is still there. Do I need to define $row['id'] I thought I had already done that in a previous query though. Link to comment https://forums.phpfreaks.com/topic/137110-error-with-mysql-code/#findComment-716170 Share on other sites More sharing options...
darkfreaks Posted December 15, 2008 Share Posted December 15, 2008 <?php $blog_id= $row['id']; $comments_sql = "SELECT name, comment FROM comments WHERE blog_id = '$blog_id' ORDER BY dateposted;"; $comments_result = mysql_query($comments_sql); ?> Link to comment https://forums.phpfreaks.com/topic/137110-error-with-mysql-code/#findComment-716189 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.