Jump to content

Recommended Posts

SQL QUERY PROBLEM IN PHP.

I m using windows xp O.S. I have installed WAMP for learning and practicing PHP.

when I execute the following code i m getting an error.I M INCLUDING A FUNCTION FILE(functions.php)

IN MY content.php file.MY ALL OTHER FUNCTIONS AND SQL QUERIES ARE WORKING EXCEPT THIS ONE.

 

 

----------------------------------------------------------------------------------------------------------------

functions.php(c:/wamp/www/widget_corp/includes/functions.php)

 

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);

if(!$result_set){

 

die("database query failed:".mysql_error());

}

//if no rows are returned, then fetch array is going to return false

if($subject = mysql_fetch_array($result_set)) {

return $subject;

} else {

return NULL;

}

 

}

 

 

-----------------------------------------------------------------------------------------------------------------

 

 

content.php(c:/wamp/www/widget_corp/content.php)

 

<?php

require("constants.php");

 

//Creating a database connection

 

$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD);

if(!$connection){

die("DATABASE CONNECTION FAILED:".mysql_error());

}

?>

 

 

<?php

 

//Selecting a database

 

$db_connect = mysql_select_db(DB_NAME,$connection);

if(!$db_connect){

die("Database selection failed:".mysql_error());

}

?>

 

 

<?php //including functions.php ?>

 

<?php require_once($_SERVER['DOCUMENT_ROOT']."/widget_corp/includes/functions.php") ?>

 

 

<?php //$_GET['subj'] and $_GET['page'] are returning my subjects and pages ids respectively. ?>

 

<?php

if(isset($_GET['subj'])){

$sel_subj = $_GET['subj'];

$sel_pg="";

}elseif(isset($_GET['page'])){

$sel_pg= $_GET['page'];

$sel_subj = "";

} else {

$sel_subj = "";

$sel_pg = "";

}

$sel_subject = get_subject_by_id($sel_subj);

$sel_page = get_page_by_id($sel_pg);

?>

 

 

------------------------------------------------------------------------------------------------------------------

 

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

Link to comment
https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/
Share on other sites

@

denno020

 

I think it will make $subject_id as a string.Well i tried it and i got just a slight change in the error. Following is the error i got:

 

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

 

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

Edited by maazzarif

@denno, you should not quote number values.

 

@mazzarrif, echo $query to see what it looks like. I think you may have a spacing issue.

 

edit.

Create query code with multiple lines

 

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

 

Not only is its structure easier to read but when you get a syntax error it tells you which line of the query. This way everything is not on line 1.

Edited by Barand

@denno, you should not quote number values.

 

@mazzarrif, echo $query to see what it looks like. I think you may have a spacing issue.

 

edit.

Create query code with multiple lines

 

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

 

Not only is its structure easier to read but when you get a syntax error it tells you which line of the query. This way everything is not on line 1.

Sir i did as you said. and this is what i got

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

 

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

Edited by maazzarif
  • 6 months later...

The error:

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

Can Be fixed, try this:

change $query .= "LIMIT 1"; to $query .= "LIMIT 0,1";
and $query .= "WHERE id=" . $subject_id ." "; to $query .= "WHERE id=" . (int)$subject_id ." ";

regards!

j0nnie, LIMIT 1 and LIMIT 0,1 are identical.

 

It is more likely that $subject_id has no value but as the OP refuses to echo $query to verify then who knows. My view is that when more info (like echo $query) is requested but the OP can't be bothered then the only thing to do is walk away.

Best to echo the query and see what it looks like.

 

Probably $subject_id contains no value or an invalid value. If $subject_id is a string, you should escape it (using mysql_real_escape_string) and put it inside quotes in the query.

[Edit]

You know you can put enters in strings too, right?

// More readable
$query = "
SELECT *
FROM subjects
WHERE id = $subject_id
LIMIT 1"
;

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.