| View previous topic :: View next topic |
| Author |
Message |
biker1000
Joined: 14 Oct 2008 Posts: 4
|
Posted: Tue Oct 14, 2008 9:25 pm Post subject: Please help newbe with this querry/syntax error! |
|
|
Hi,
I'm trying to use this function-
| Code: |
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 ($subect = mysql_fetch_array($result_set)) {
return $subject; } else {return NULL; }
}
|
And get error from there in browser :
"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".
I'really dont know what that means - this code supposed to be working - but it doesn't.( I checked the manual and syntax seems ok..I'm clueless.
Can anybody see what am I doing wrong?
Thanks
 |
|
| Back to top |
|
 |
lostboy

Joined: 02 May 2004 Posts: 6033 Location: toronto, canada
|
Posted: Tue Oct 14, 2008 10:48 pm Post subject: |
|
|
| echo out the query and run it thru the gui of your choice and see what it says |
|
| Back to top |
|
 |
biker1000
Joined: 14 Oct 2008 Posts: 4
|
Posted: Wed Oct 15, 2008 12:03 am Post subject: |
|
|
| lostboy wrote: | | echo out the query and run it thru the gui of your choice and see what it says |
If I change it like this :
function get_subject_by_id($subject_id) {
global $connection;
//$query = "SELECT * FROM subjects WHERE id = {$subject_id}";
$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;}
}
and echo out( by passing return value to external code)
this is what I get in browser:
"Database query failed: 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 '' at line 1"
No mater how I tweak it - this is what I get. |
|
| Back to top |
|
 |
lostboy

Joined: 02 May 2004 Posts: 6033 Location: toronto, canada
|
Posted: Wed Oct 15, 2008 5:14 am Post subject: |
|
|
| Code: | $query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id= {$subject_id} ";
$query .= "LIMIT 1";
echo $sql;
|
|
|
| Back to top |
|
 |
biker1000
Joined: 14 Oct 2008 Posts: 4
|
Posted: Wed Oct 15, 2008 12:56 pm Post subject: |
|
|
| lostboy wrote: | | Code: | $query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id= {$subject_id} ";
$query .= "LIMIT 1";
echo $query;
|
|
Then I get:
Undefined variable: subject_id in C:\wamp\www\PHP_Sandbox\test_query.php on line 33
SELECT * FROM subjects WHERE id =
But id is a field in table subjects why should I define it as variable?! |
|
| Back to top |
|
 |
lostboy

Joined: 02 May 2004 Posts: 6033 Location: toronto, canada
|
Posted: Wed Oct 15, 2008 3:53 pm Post subject: |
|
|
| $Subject_id has no value, its not showing in the query which is likely what the real problem is |
|
| Back to top |
|
 |
biker1000
Joined: 14 Oct 2008 Posts: 4
|
Posted: Sat Oct 18, 2008 1:11 pm Post subject: |
|
|
| lostboy wrote: | | $Subject_id has no value, its not showing in the query which is likely what the real problem is |
Thanks!
I'll have to go through my code and find out why... |
|
| Back to top |
|
 |
|