Jump to content

Recommended Posts

I keep on getting this error message on my function:

 

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.

 

The function itself is:

function get_page_by_id($page_id) {
    global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_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;}
}

 

My question is : where exactly is this mistake with SQL then? If I remove "LIMIT 1" I still get the  message that there's something wrong with query.

 

Thanks in advance

Just echo $query to see what is in it. $page_id is probably empty.

 

You right it's empty. And I don't know why. This is how it is being set:

<?php
if (isset($_GET['subj'])) {
	$sel_subj = $_GET['subj'];
	$sel_page = "";
} elseif (isset($_GET['page'])) {
	$sel_subj = "";
	$sel_page = $_GET['page'];
} else {
	$sel_subj = "";
	$sel_page = "";

}
$sel_subject = get_subject_by_id($sel_subj); 
?>

$sel_sub is passed to here

function get_subject_by_id($subject_id)
{

$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id;
//$query .= "LIMIT 1";
$result_set = mysql_query($query);
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;}
}

 

but it cannot be empty!

You changed which code you are talking about.

 

Anyway, is the parameter a string or a number? If it is a string (the code allows/sets an empty "" for a value), then it must be enclosed in single quotes in the query.

You changed which code you are talking about.

 

Anyway, is the parameter a string or a number? If it is a string (the code allows/sets an empty "" for a value), then it must be enclosed in single quotes in the query.

 

It is integer.

Try this,

 

function get_page_by_id($page_id) {

    global $connection;

$query = "SELECT * ";

$query .= "FROM pages ";

$query .= "WHERE id= ' " . $page_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;}

}

 

*put where id = '' like this....

Try this,

 

function get_page_by_id($page_id) {

    global $connection;

$query = "SELECT * ";

$query .= "FROM pages ";

$query .= "WHERE id= ' " . $page_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;}

}

 

*put where id = '' like this....

 

I changed it like this:

function get_subject_by_id($subject_id) {
    global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$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;}
}

 

Because here

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_GET['subj'])) {
	$sel_subj = $_GET['subj'];
	$sel_page = "";
} elseif (isset($_GET['page'])) {
	$sel_subj = "";
	$sel_page = $_GET['page'];
} else {
	$sel_subj = "";
	$sel_page = "";
}
$sel_subject = get_subject_by_id($sel_subj);
?>
this function puts there $sel_subj.

But I still get "Unknown column 'subject_id' in 'where clause'" error. Which doesn't make sense, because $sel_subj is not empty - it always has integer subject id in it.

Maybe I should try to send you the whole code if that would make it easier to track?

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.