Syntax Error, But can't find it.

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
OoZI
Posts: 109
Joined: Mon Jan 02, 2012 4:22 pm

Syntax Error, But can't find it.

Post by OoZI »

Here is my Code:

Code: Select all

  
<?php
session_start();
include ('header.php');

$db = mysql_connect("mysql2.000webhost.com","a7118719_agents","********","a7118719_agents") or die("Could Not Connect");
if(!$db)
die("No Database");
if(!mysql_select_db("a7118719_agents",$db))
die("No Database Selected");

$to = $_POST['to'];
$pri = $_POST['pri'];
$message = $_POST['message'];
$from = $_SESSION['name'];

$mQ = "INSERT INTO messages (to, from, message, priority) VALUES ('$to','$from','$message','$pri')";

$mS = mysql_query($mQ) or die ("ERROR ID: 1");

echo "Message Sent!";

?>





It continues returning Error 1, so it can't do the Query... Why not?
-OoZI

My Blog
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Syntax Error, But can't find it.

Post by Torniquet »

do

Code: Select all

$mS = mysql_query($mQ) or die (mysql_error()); 
it should give you the reason why your query is failing.
New Site Coming Soon! Stay tuned :D
User avatar
OoZI
Posts: 109
Joined: Mon Jan 02, 2012 4:22 pm

Re: Syntax Error, But can't find it.

Post by OoZI »

So I did that, and I got this.

Code: Select all

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 'to, from, message, priority) VALUES ('David Faulkner','Kyle Dawson','This is a T' at line 1
I still can't find the error...
-OoZI

My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Syntax Error, But can't find it.

Post by Jackolantern »

You have used 2 different MySQL reserved words in your query: "to" and "from". You cannot use these as names of anything in your database unless you put `back-ticks` around them in the query, but even then it is highly suggested you do not use them. Keeping them sets you up for headaches, since every query will fail that contains those names when not back-ticked.

I don't know why MySQL doesn't give better error messages when you use reserve words, but it probably has to do with performance gains by not parsing the query before execution. You can see in your error message the query broke when it hit "to", so when you see that, or you just can't debug a query, always check the rather extensive list of MySQL reserve words I linked above to make sure you aren't using any of them :)
The indelible lord of tl;dr
User avatar
OoZI
Posts: 109
Joined: Mon Jan 02, 2012 4:22 pm

Re: Syntax Error, But can't find it.

Post by OoZI »

*facepalm* I would have never thought of that... Thanks.
-OoZI

My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Syntax Error, But can't find it.

Post by Jackolantern »

OoZI wrote:*facepalm* I would have never thought of that... Thanks.
Most people wouldn't because of the poor error messages for reserve word violations if they hadn't seen one before. Don't feel bad :)
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”