mediaWorks Posted October 24, 2008 Share Posted October 24, 2008 Hi all, I hope I'm posting in the right forum.... I'm trying to learn PHP & MySQL and am currently taking a lynda.com tutorial. The tutorial has been really awesome so far but I've run into a problem with one of the lessons. The code is generating an error and I can't figure out why....its so frustrating! Can anybody help? The code is: <?php // 3. Pull All Subjects from Database $subject_set = mysql_query("SELECT * FROM subjects", $connection); if (!$subject_set) { die("Database query failed: " . mysql_error()); } // 4. Output list of all subjects to page while ($subject = mysql_fetch_assoc($subject_set)) { echo "<li>{$subject["menu_name"]}</li>"; } // 3. Pull all Pages from Database $page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection); if (!$page_set) { die("Database query failed: " . mysql_error()); } // 4. Output list of all pages echo "<ul class=\"pages\">"; while ($page = mysql_fetch_assoc($page_set)) { echo "<li>{$page["menu_name"]}</li>"; } echo "</ul>"; ?> The problem seems to be: $page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection); But its generating the following error message: 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 '' at line 1 I'm using php version 5.2.5 and MySQL version 5.0.45. This is my first time posting, so apologizes in advance if I didn't provide enough information or something. Thank you so much! (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/ Share on other sites More sharing options...
johnsmith153 Posted October 25, 2008 Share Posted October 25, 2008 Have you looked at Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/#findComment-674125 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2008 Share Posted October 25, 2008 I would change <?php $page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection); if (!$page_set) { die("Database query failed: " . mysql_error()); } ?> to <?php $q = "SELECT * FROM pages WHERE subject_id = '{$subject['id']}'" $page_set = mysql_query($q) or die("Database query failed: $q<br>" . mysql_error()); ?> This will show you the query. Ken Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/#findComment-674129 Share on other sites More sharing options...
mediaWorks Posted October 25, 2008 Author Share Posted October 25, 2008 Ken, Thank you so much for your super quick response however replacing that code resulted in: syntax error, unexpected T_VARIABLE in C:\wamp\www\widget_corp\testing.php on line 22 line 22 is: $page_set = mysql_query($q) or die("Database query failed: $q<br>" . mysql_error()); Any other ideas? Thanks again! Sara Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/#findComment-674137 Share on other sites More sharing options...
darkfreaks Posted October 25, 2008 Share Posted October 25, 2008 $page_set = mysql_query($q) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/#findComment-674174 Share on other sites More sharing options...
kenrbnsn Posted October 26, 2008 Share Posted October 26, 2008 I just forgot the terminating semi-colon on the previous line: <?php $q = "SELECT * FROM pages WHERE subject_id = '{$subject['id']}'"; $page_set = mysql_query($q) or die("Database query failed: $q<br>" . mysql_error()); ?> Ken Link to comment https://forums.phpfreaks.com/topic/130018-php-and-sql-basic-help/#findComment-675081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.