Hi,

I'm currently learning PHP, MySQL and Apache through a book I got (Beginning PHP5, Apache, MySQL Web Development, Publisher: Wrox (2005)) which was written by some people from this forum, so I was hoping to get some help with an example in it:

Now I am working through how to make PHP look for a database and then populate it (page 94 if you have the book) I followed everything in the book, apart from the fact that I installed AMP through XAMPP because it saved me a hell of alot of time. However I keep getting the following error when I run it, and I can't seem to find the error in either my coding, or whatnot.

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 8

Here is the Code, I am using DreamWeaver as my 'environment' because of the handy Auto-Complete, but I've tried running from Crimson editor, yet it still doesn't work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
	//connects to the server with my Username
	$connect = mysql_connect("localhost", "Denis", "denis") or die ("Error with server connection");

//creates a database if it does not exist
$create = mysql_query("CREATE DATABASE IF NOT EXISTS moviesite") or die (mysql_error());
//makes sure database above is the active one
mysql_select_db("moviesite");

//creates a "movie" table
$movie = "CREATE TABLE movie (
	movie_id int(11) NOT NULL auto_increment,
	movie_name varchar(255) NOT NULL,
	movie_type tinyint(2) NOT NULL default 0,
	movie_year int(4) NOT NULL default 0,
	movie_leadactor int(11) NOT NULL default 0,
	movie_director int(11) NOT NULL default 0,
	)";

$results = mysql_query($movie) or die (mysql_error());

//creates "movietype" table
$movietype = "CREATE TABLE movietype (
	movietype_id int(11) NOT NULL auto_increment,
	movietype_label varchar(100) NOT NULL,
	PRIMARY KEY (movietype_id)
	)";

$results = mysql_query($movietype) or die (mysql_error());

//create a "people" table
$people = "CREATE TABLE people (
	people_id int(11) NOT NULL auto_increment,
	people_fullname varchar(255) NOT NULL,
	people_isactor tinyint(1) NOT NULL default 0,
	people_isdirector tinyint(1) NOT  NULL default 0,
	PRIMARY KEY (people_id)
	)";

$results = mysql_query($people) or die (mysql_error());

echo "Movie database successfully created!";

?>
</body>
</html>

Thanks heaps for that. And also, I was wondering. When the Apache server tells me my error, in the line count - does it refer to the HTML lines, or since they aren't processed by the server are they just ignored, and is the 8th PHP code line the one on which the error is, if so, then the error must be:

mysql_select_db("moviesite");

However I can't see what's wrong... 🙁

    When it's a PHP error, it refers to the line number of the file. In this case, however, it's a MySQL error being reported via a call to mysql_error(), so the line number is in reference to the actual SQL being submitted. The problem is that you have a comma after the last column definition in this query, which implies another column should follow:

    	$movie = "CREATE TABLE movie (
    		movie_id int(11) NOT NULL auto_increment,
    		movie_name varchar(255) NOT NULL,
    		movie_type tinyint(2) NOT NULL default 0,
    		movie_year int(4) NOT NULL default 0,
    		movie_leadactor int(11) NOT NULL default 0,
    		movie_director int(11) NOT NULL default 0,   // <--- this comma
    		)";
    

    Just get rid of the last comma, and that problem should be taken care of.

      Ah, that fixed it. Thanks heaps! Crazy how an extra comma can crash a whole program... I'll pay more attention to it now. Thanks again 🙂

        Yep, you're not in HTML-land any more. Now you're in the world of programming where every character must be correct. :evilgrin:

          Don't forget to mark this thread resolved (using the link on the Thread Tools menu).

            Write a Reply...