Jump to content

PHP and SQL basic help


mediaWorks

Recommended Posts

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

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

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

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.