Jump to content

getting weird error..


jocabergs

Recommended Posts

okay, I'm getting an error on this particular function, something about the syntax in the mysql LIMIT statement, but as far as I can see I have it right, currently using easymysql3.0 as test server, which was latest version as of a couple weeks ago.

 

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .="FROM subjects ";
$query .="WHERE id=" . $subject_id . " ";
$query .="LIMIT 1";	//this is the line I'm having problems with comes up as syntax error   ***this is where problem is at least the one I know about***
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//if no rows are returned fetch array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
	return NULL;
	}
}

 

I'd really appreciate the help, thanks for looking at it.

Link to comment
Share on other sites

next time, "something about the syntax in the mysql LIMIT statement" won't suffice.  consider this:  the more information you give someone to help solve your problem, the better, correct?  these error messages are important, and can help some people solve an issue in the blink of an eye.

 

change this line:

 

$result_set = mysql_query($query, $connection);

 

to:

 

$result_set = mysql_query($query, $connection) or trigger_error (mysql_error());

 

note the trigger_error() with the mysql_error() argument.  this will display any applicable errors within your query.

Link to comment
Share on other sites

"Full error message"

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 0,1' at line 1 in C:\web\widgetcorp\includes\functions.php on line 29

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

 

try replacing your $query with this

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

 

I did replace that string, with your code, but it still returned the same error. 

Its got something to do with the LIMIT statement in sql, or I've totally screwed up something else, I guess what I'm asking is am I crazy or is that syntax correct?

 

next time, "something about the syntax in the mysql LIMIT statement" won't suffice.  consider this:  the more information you give someone to help solve your problem, the better, correct?  these error messages are important, and can help some people solve an issue in the blink of an eye.

 

change this line:

 

$result_set = mysql_query($query, $connection);

 

to:

 

$result_set = mysql_query($query, $connection) or trigger_error (mysql_error());

 

note the trigger_error() with the mysql_error() argument.  this will display any applicable errors within your query.

 

It just repeated the same error, sorry for not being more clear earlier and thanks for your help.

 

 

UPDATE: I found the error, thanks for your help.  The error had nothing to do with the MYSQL query it had to do with me changing a couple ambiguous variable names on my content page and missing one of them.  I still wonder why it specifically pointed to the query though.. oh well working now, thanks for taking your time to look at it.

 

Link to comment
Share on other sites

is $subject_id set?  put single quotes around it in the query:

 

<?php
function get_subject_by_id($subject_id)
{
global $connection;

$query = "
	SELECT *
	FROM `subjects`
	WHERE `id` = '{$subject_id}'
	LIMIT 1
";
$result_set = mysql_query($query, $connection) or trigger_error (mysql_error());

confirm_query($result_set);
//if no rows are returned fetch array will return false
if ($subject = mysql_fetch_array($result_set)) {
	return $subject;
} else {
	return $query;
    }
}
?>

 

i changed up your query a bit.  easier to read.  i also have it returning $query so you can also see the $query on fail.

 

NOTE: keep backticks ` surrounding field names as is (or remove them, but they are good practice), and do not replace them with single-quotes.  i put them there on purpose, and they are not incorrect as a lot of people seem to think they are, they replace them with single-quotes and their query fails.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.