php 如果不存在则创建表 - 意外的 T_STRING
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3248672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create table if not exist - unexpected T_STRING
提问by michaelcarrano
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.
我在尝试为我正在处理的网站创建表格时遇到问题,这是我正在使用的数据库代码,但在第 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
回答by MacAnthony
Your SQL command is not php code. It's the php parser that's giving the error trying to interpret that code.
您的 SQL 命令不是 php 代码。正是 php 解析器在尝试解释该代码时给出了错误。
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
请参阅此处的示例:http: //www.php.net/manual/en/book.mysqli.php
回答by Jan
First the SQL should look like this:
首先,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;
and second - you have to pass it to mysql_query(); function call. Of course you have to connect to the DB first.
其次 - 你必须将它传递给 mysql_query(); 函数调用。当然,您必须先连接到数据库。
回答by ryanzec
You need to wrap that query is double quotes to store it as a string (and then run the query in MySQL through PHP).
您需要将该查询包装为双引号以将其存储为字符串(然后通过 PHP 在 MySQL 中运行该查询)。

