chaseman Posted February 5, 2011 Share Posted February 5, 2011 Basically I have a drop down list where you can choose to ORDER by DATE either by ASC or DESC and then click submit. This is the script to do that: $sort_date_var = $_REQUEST['sort_date']; // SORT CATEGORY BLOCK if ($sort_date_var) { // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Retrieve the chosen category from MySQL $query4 = "SELECT * FROM asciiart ORDER BY created_date '$sort_date_var'"; $data4 = mysqli_query($dbc, $query4); //Loop through the array of data while ($row4 = mysqli_fetch_array($data4) or die (mysqli_error($dbc))) { echo "<table class='asciiartTable'>"; // Display the score data echo "<tr><td class='asciiart_name'>"; echo "<strong>" . htmlentities($row4['asciiart_name']) . "</strong><br /></td></tr>"; echo "<tr><td class='asciiart_contribution'><pre>" . htmlentities($row4['asciiart_contribution']) . "</pre><br /></td></tr>"; echo "<tr><td class='asciiart_categoryDate'>" . htmlentities($row4['asciiart_category']) . " | " . date('M d, Y', strtotime($row4['created_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } I just put an "or die" behind the mysqli_query to get the correct error message which is as follows: 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 ''DESC'' at line 1 $sort_date_var is either ASC or DESC chosen by the drop down list. BUT when I use the same query in the SQL command line it works, it simply doesn't work in the PHP script. Another error message I'm getting is: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\xampp\htdocs\knuffix\exp\02\asciiart_list.php on line 70 The thing is I'm using the same script to SORT BY CATEGORY, the only difference is the query everything else is more or less the same, so in that sense there shouldn't be anything wrong with mysqli_fetch_array(). Any ideas what the mistake here is? Link to comment https://forums.phpfreaks.com/topic/226814-getting-a-sql-syntax-error/ Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2011 Share Posted February 5, 2011 DESC doesn't get enclosed in quotes. Link to comment https://forums.phpfreaks.com/topic/226814-getting-a-sql-syntax-error/#findComment-1170363 Share on other sites More sharing options...
chaseman Posted February 5, 2011 Author Share Posted February 5, 2011 Thank you it's working now! Link to comment https://forums.phpfreaks.com/topic/226814-getting-a-sql-syntax-error/#findComment-1170415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.