Create table if not exist - unexpected T_STRING
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 开发者_StackOverflowline 2
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
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.
You need to wrap that query is double quotes to store it as a string (and then run the query in MySQL through PHP).
精彩评论