Jump to content

can anybody help me with this query function?


phpnewbe

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.

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?

Archived

This topic is now archived and is closed to further replies.

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