Jump to content

Getting a SQL Syntax Error!


chaseman

Recommended Posts

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

Archived

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

×
×
  • Create New...

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.