views:

38

answers:

3

Hi,

I am having an issue trying to create a table for a website I am working on, here is the database code I am using but I get an error at line 2.

<?php
   CREATE TABLE IF NOT EXISTS 'speakers' (
   `id` int(11) unsigned NOT NULL auto_increment,
   `name` varchar(255) NOT NULL default '',
   `image` varchar(255) NOT NULL default '',
   `bio` varchar(500) NOT NULL default '',
   `position` int(10) unsigned NOT NULL default '0',
   `status` tinyint(2) NOT NULL default '1',
   PRIMARY KEY  (`id`)
   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8
?>

I simply cannot find where I have my issue and can use your help.

Here is the error I get:

Parse error: syntax error, unexpected T_STRING in /public_html/create_db.php on line 2
A: 

First the SQL should look like this:

CREATE TABLE IF NOT EXISTS `speakers` (
    `id` int(11) unsigned NOT NULL auto_increment,
    `name` varchar(255) NOT NULL default '',
    `image` varchar(255) NOT NULL default '',
    `bio` varchar(500) NOT NULL default '',
    `position` int(10) unsigned NOT NULL default '0',
    `status` tinyint(2) NOT NULL default '1',
    PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

and second - you have to pass it to mysql_query(); function call. Of course you have to connect to the DB first.

Kaltas
A: 

You need to wrap that query is double quotes to store it as a string (and then run the query in MySQL through PHP).

ryanzec
+3  A: 

Your SQL command is not php code. It's the php parser that's giving the error trying to interpret that code.

Should be something like:

<?php
   $sql = "CREATE TABLE IF NOT EXISTS `speakers` (
   `id` int(11) unsigned NOT NULL auto_increment,
   `name` varchar(255) NOT NULL default '',
   `image` varchar(255) NOT NULL default '',
   `bio` varchar(500) NOT NULL default '',
   `position` int(10) unsigned NOT NULL default '0',
   `status` tinyint(2) NOT NULL default '1',
   PRIMARY KEY  (`id`)
   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

   mysql_query($sql);
?>

See examples here: http://www.php.net/manual/en/book.mysqli.php

MacAnthony
Introducing newcomers to the mysql extension instead of the mysqli or PDO extensions is probably a bad idea...
Charles
It's been so long since I've looked through the php manual, I had forgotten they split them out when they reorganized. I changed the link, but I think either would have helped with concept understanding that seemed to be missing.
MacAnthony
Plus backticks for ``speakers``
Mark Baker
Good catch, Mark, I didn't notice the use of quotes instead at first.
MacAnthony