Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Could somebody tell me what is wrong with my syntax?

syntax

  • Please log in to reply
8 replies to this topic

#1 doheja07

doheja07

    CC Regular

  • Just Joined
  • PipPipPip
  • 31 posts

Posted 08 May 2009 - 01:28 PM

I am in the process of learning php. I am following a tutorial. I have gotten to a point where I am trying to perform a database query. When I try to test it, I get a message that says there is something wrong with my syntax. I have studied the way is looks in the tutorial and mine looks exactly the same. The tutorial was made a couple years ago so I am thinking that may be playing a significant part in the problem. I am using mysql version 5.1.33-community. This is what the syntax looks like:


$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";


I am thinking it has to do with the $subject_id part because that is the only thing that has changed from previous database queries which work fine.

Edited by Jordan, 08 May 2009 - 01:43 PM.
Please use code tags

  • 0

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 May 2009 - 01:46 PM

I don't see anything wrong with the Syntax in what you've posted. Can you paste more and paste the exact error? You can also rewrite that string:


$query = "SELCT * "
. "FROM subjects "
. "WHERE id={$subject_id} "
. "LIMIT 1"
;


Or, all on one line:


$query = "SELECT * FROM subjects WHERE id={$subject_id} LIMIT 1";

  • 0

#3 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 May 2009 - 01:53 PM

it's always good to enclose variables with apostrophes in mysql atleast, and make sure the data is ran through mysql_real_escape_string(), if they are taken from a user atleast...

$query = "SELECT * FROM subjects WHERE id='". mysql_real_escape_string($subject_id) ." LIMIT 1";

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 May 2009 - 01:55 PM

Good suggestion, Orjan. But that wouldn't cause a syntax error. I think it is coming from somewhere else. I suppose not enclosing the variable in quotes could cause a MySQL syntax error.
  • 0

#5 doheja07

doheja07

    CC Regular

  • Just Joined
  • PipPipPip
  • 31 posts

Posted 08 May 2009 - 01:56 PM

Here is the coding for the function.

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}

This is the error message I get.

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 'LIMIT 1' at line 1

Edited by Jaan, 09 May 2009 - 05:01 AM.
Please use code tags when you're posting your codes!

  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 May 2009 - 02:01 PM

yeah, it's probably the subject id that's not proper. try to put apostrophes around it like this:

$query .= "WHERE id='" . $subject_id ."' ";


you can also try to do this to show yourself the sql code on error:

@mysql_query($sql, $connection) or die("SQL: ".$sql."<br>Error: ".mysql_error());


That way, it prints the full sql clause to you along with the error.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 May 2009 - 02:04 PM

mysql wants apostrophes around every data but numbers, but they allow that as well.
  • 1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#8 doheja07

doheja07

    CC Regular

  • Just Joined
  • PipPipPip
  • 31 posts

Posted 08 May 2009 - 02:30 PM

Ok. Thank you everybody. I changed the code to this.


$query .= "WHERE id='" . $subject_id ."' ";


That worked for me.

Edited by Jaan, 09 May 2009 - 05:02 AM.
Please use code tags when you're posting your codes!

  • 0

#9 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 May 2009 - 03:16 PM

Nice work Orjan, +rep to ya!
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download