Jump to content

syntex error??


puremagnum

Recommended Posts

im new to php/mysql so this hopefully is a easy question:whats wrong with my syntex??.

 

this is the code that didn't work:

$page_set= mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}",$connection );

 

but this did work:

$page_set= mysql_query("SELECT * FROM pages",$connection );

 

what this is the error im getting when i (attempt to) load the pg

 

"DB 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"

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/
Share on other sites

You're already getting into a bad habit of embedding the query string in the query execution. Store the query string in a variable so you can echo it when you have problems with it, like now.

so pikachu, u saying its bad practice to do it like that??..hmm.. well iv been following a step by step traning vids(prolly outdated??).. so im just lost on how it can be bad practice

 

and yes MMDE im pretty sure i got something assigned to it

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145979
Share on other sites

He means you should:

 

$query="SELECT * FROM pages WHERE subject_id = $subject['id']";
echo $query;
$page_set=mysql_query($query) or die(mysql_error());

 

and I agree

 

that it says there is something wrong right next to '', usually means that there is nothing at the end and it expects there to be something there, which is why I wonder if $subject has anything assigned to it!

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145985
Share on other sites

thanks that worked.. not getting the same error any more..

but now im getting

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in X:\CCC\WWWW\folder\pg.php on line 28"

 

$query="SELECT * FROM pages WHERE subject_id = $subject['id']";

 

im srry for the hassle lol... told u im new  :P

 

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145990
Share on other sites

You're already getting into a bad habit of embedding the query string in the query execution. Store the query string in a variable so you can echo it when you have problems with it, like now.

so pikachu, u saying its bad practice to do it like that??..hmm.. well iv been following a step by step traning vids(prolly outdated??).. so im just lost on how it can be bad practice

 

and yes MMDE im pretty sure i got something assigned to it

 

I guess I am saying it's bad practice, especially for a beginner. By doing it that way, you eliminate the possibility of using one of the more important methods of debugging, echoing the query string.

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146003
Share on other sites

Assuming it's not a string:

$query="SELECT * FROM pages WHERE subject_id = {$subject['id']}";

new error now im going nuts lol

SELECT * FROM pages WHERE subject_id = 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

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146005
Share on other sites

Now is when echoing the query (along with the error that was generated) will come in handy . . .

 

$query="SELECT * FROM pages WHERE subject_id = $subject['id']";
echo $query;
$page_set=mysql_query($query) or die( "<br>Query string: $query<br>Produced error: " . mysql_error() );

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146006
Share on other sites

Also, good practice is to check if you actually got any rows from the query:

if(mysql_num_rows($page_set)>0){

}

 

i put in the end of the while loop is that ok??.. and it didnt make a difference

 

if you look above now, you will see I found what was wrong.

 

putting it at the end of the loop would be a waste, the point of it being to check before you start fetching from it, to see if there are any rows, but it doesn't really make any difference in this case though.

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146020
Share on other sites

No, it won't make a difference. Let's get the parse errors taken care of before we try jacking with anything else, shall we?

 

problem is he didn't use single quotes for the array key...

echo "<li>{$page["menu_name"]}</li>";

should be:

echo "<li>$page['menu_name']</li>";

Link to comment
https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146023
Share on other sites

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.