Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 22nd, 2004, 10:15 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default $sql="CREATE TABLE syntax error

I create this file listed below but I am getting this error when I run it:

Can't create mp3_files table 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 'Name VARCHAR, Artist VARCHAR , Release_Date date, Additional

The syntax looks good to me does someone see something that I am missing??


<?php

$host= "localhost";
$dbuser= "unsernamer";
$dbpass= "password";
$database= "mp3_db";
$table= "mp3_files";


$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ' . mysql_error());



if (! @mysql_select_db($database,$link)){

mysql_query("CREATE DATABASE $database");

}
else
{
$sql="CREATE TABLE $table (
id int NOT NULL AUTO_INCREMENT Primary Key,
Song Name VARCHAR,
Artist VARCHAR ,
Release_Date date,
Additional_Notes VARCHAR,
Path VARCHAR )";

$query1 = mysql_query($sql);


if ($query1){
print "<p>$table table created</p>";
} else {
$error = 1;
echo("<p>Can't create $table table ". mysql_error() ."<p>");
}


}

?>

  #2 (permalink)  
Old August 23rd, 2004, 04:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you want to put in fieldnames with spaces in them, you'll have to surround them with backticks (on the English-language versions of a PC keyboard, this is usually up in the top left hand corner of the keyboard - the one with the "¬" character on it). You'll also have to use backticks whenever you are working with that field in your code. If you use a tool like PHPMyAdmin, you'll probably see that it surrounds the naames of every database entity in backticks for that very reason.

Take it easy,
Dan
  #3 (permalink)  
Old August 23rd, 2004, 12:46 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Don't you also have to specify the size of a VARCHAR field?

Snib

<><
  #4 (permalink)  
Old August 24th, 2004, 04:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hm, yes, you do. The actual error reported was generated by the space, but you're right; adding backticks would, indeed, have only allowed the CREATE statement to proceed on to that second comma before it bombed, again, well spotted.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in SQL syntax phantom3008 ASP.NET 1.0 and 1.1 Basics 1 April 26th, 2007 07:00 AM
Example SQL causes syntax error Tom Rigby BOOK: Beginning SQL 1 April 18th, 2007 09:32 AM
CREATE TABLE syntax error RaCheer SQL Language 1 January 29th, 2006 01:46 AM
Create table statement syntax error in access 2000 method Access 1 August 8th, 2005 02:10 AM
What is the syntax of create table and insert stat method Access 1 June 27th, 2005 02:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.