MySQL Forums
Forum List  »  PHP

Database query failed: You have an error in your SQL syntax
Posted by: Amandine M.
Date: July 24, 2012 09:25AM

I have looked every where I know for an answer I'm at a lost here !
I'm a beginner and following the Lynda.com training : "PHP With MySQL Essential Training" it is a very old training(2007) but the only one I could find up until Mid Chapter 12 all was fine when I came across a syntax php wouldn't read

I have the latest version of XAMPP installed !

the NOTICE :
-> Notice: Undefined index: id in C:\xampp\htdocs\Essential.PHP\the_world_corp\content.php on line 44
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 'ORDER BY position ASC' at line 4 <-

using a database with 2 simple tables :
--

CREATE TABLE IF NOT EXISTS `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject_id` int(11) NOT NULL,
`menu_name` varchar(30) NOT NULL,
`position` int(3) NOT NULL,
`visible` tinyint(1) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--

CREATE TABLE IF NOT EXISTS `subjects` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`menu_name` varchar(30) NOT NULL,
`position` int(3) NOT NULL,
`visible` tinyint(3) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

my PAGE is !
<?php require("includes/constants.php");

// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
?>

<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php

$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if (!$subject_set) {
die("Database query failed: " . mysql_error());
}

while ($subject = mysql_fetch_array($subject_set)) {
echo "<li>{$subject["menu_name"]}</li>";
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);// THIS IS THIS IS WHERE THE PROBLEM LIES
if (!$page_set) {
die("Database query failed: " . mysql_error());
}
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li>{$page["menu_name"]}</li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<h2>Content Area</h2>

</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>


THANK YOU FOR your HELP

Options: ReplyQuote


Subject
Written By
Posted
Database query failed: You have an error in your SQL syntax
July 24, 2012 09:25AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.