puremagnum Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 Double quotes within a double-quoted string must be escaped with a backslash. However, since it's an array index, you'd be better off to change them to single quotes: $array['index'] Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145959 Share on other sites More sharing options...
Anti-Moronic Posted December 12, 2010 Share Posted December 12, 2010 syntex error? I'm not surprised. {$subject["id"]} should be {$subject['id']} ..for reasons Pikachu mentioned about. Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145960 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 thx for the quick replie.. did what u suggested... but still getting the same error as b4 i posted... grrr Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145966 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 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. Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145972 Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 Have you tried to see if something actually is assigned to $subject['id']? Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145973 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 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 More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 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 Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145990 Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 oh and, I hope the $subject['id'] is a number! if it is not a number, put quotes around it: $query="SELECT * FROM pages WHERE subject_id = '$subject['id']'"; Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145994 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 oh and, I hope the $subject['id'] is a number! if it is not a number, put quotes around it: $query="SELECT * FROM pages WHERE subject_id = '$subject['id']'"; yeah the subject id it is a number Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1145997 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 Assuming it's not a string: $query="SELECT * FROM pages WHERE subject_id = {$subject['id']}"; Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146000 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 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 More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 now im getting Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' on line 29 good news tho... we moved down a line lol srry didny mean to double post.. u can earse this one Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146009 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in X:\WWW\YYYY\blahh\pg.php on line 29 line 29 is $query="SELECT * FROM pages WHERE subject_id = $subject['id']"; Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146010 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 OK. Paste in the code from line 27 through 31. Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146011 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 line 27 - 32... $page_set=mysql_query($query) or die( "<br>Query string: $query<br>Produced error: " . mysql_error() ); echo"<ul class=\"pages\">"; while($page= mysql_fetch_array($page_set)) { echo "<li>{$page["menu_name"]}</li>"; } Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146012 Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 Also, good practice is to check if you actually got any rows from the query: if(mysql_num_rows($page_set)>0){ } use single quotes for the array key: echo "<li>$page['menu_name']</li>"; Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146015 Share on other sites More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 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 Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146018 Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 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? Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146021 Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 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 More sharing options...
puremagnum Posted December 12, 2010 Author Share Posted December 12, 2010 aame error but now i got the error on line 23 now witch is echo "<li>$page['menu_name']</li>"; Link to comment https://forums.phpfreaks.com/topic/221355-syntex-error/#findComment-1146024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.