php文件创建mysql的表
一个php文件,里面内容是建表语句,如下,怎么操作这个php文件才能在mysql中建表啊?CREATETABLEIFNOTEXISTS`{$installer->getT...
一个php文件,里面内容是建表语句,如下,怎么操作这个php文件才能在mysql中建表啊?
CREATE TABLE IF NOT EXISTS `{$installer->getTable('catalog_category_description')}` (
`category_id` mediumint(8) unsigned NOT NULL,
`language_id` smallint(5) unsigned NOT NULL,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`meta_title` varchar(128) NOT NULL,
`meta_description` text NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`category_id`,`language_id`),
KEY `i_language_id` (`language_id`),
CONSTRAINT `fk_category_id` FOREIGN KEY (`category_id`) REFERENCES `{$installer->getTable('catalog_category')}` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_language_id` FOREIGN KEY (`language_id`) REFERENCES `{$installer->getTable('locale_language')}` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; 展开
CREATE TABLE IF NOT EXISTS `{$installer->getTable('catalog_category_description')}` (
`category_id` mediumint(8) unsigned NOT NULL,
`language_id` smallint(5) unsigned NOT NULL,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`meta_title` varchar(128) NOT NULL,
`meta_description` text NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`category_id`,`language_id`),
KEY `i_language_id` (`language_id`),
CONSTRAINT `fk_category_id` FOREIGN KEY (`category_id`) REFERENCES `{$installer->getTable('catalog_category')}` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_language_id` FOREIGN KEY (`language_id`) REFERENCES `{$installer->getTable('locale_language')}` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; 展开
2个回答
展开全部
首先你把这个sql语句赋给一个字符串变量:
$sql="CREATE TABLE IF NOT EXISTS `{$installer->getTable('catalog_category_description')}` (
`category_id` mediumint(8) unsigned NOT NULL,
`language_id` smallint(5) unsigned NOT NULL,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`meta_title` varchar(128) NOT NULL,
`meta_description` text NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`category_id`,`language_id`),
KEY `i_language_id` (`language_id`),
CONSTRAINT `fk_category_id` FOREIGN KEY (`category_id`) REFERENCES `{$installer->getTable('catalog_category')}` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_language_id` FOREIGN KEY (`language_id`) REFERENCES `{$installer->getTable('locale_language')}` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC";
然后连接mysql数据库,执行这个语句:
mysql_connect("localhost","root","");
mysql_select_db("dbname");
mysql_query($sql);
就可以了。
注意连接数据库帐号的权限要有创建表的权限,否则创建就不会成功!
$sql="CREATE TABLE IF NOT EXISTS `{$installer->getTable('catalog_category_description')}` (
`category_id` mediumint(8) unsigned NOT NULL,
`language_id` smallint(5) unsigned NOT NULL,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`meta_title` varchar(128) NOT NULL,
`meta_description` text NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`category_id`,`language_id`),
KEY `i_language_id` (`language_id`),
CONSTRAINT `fk_category_id` FOREIGN KEY (`category_id`) REFERENCES `{$installer->getTable('catalog_category')}` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_language_id` FOREIGN KEY (`language_id`) REFERENCES `{$installer->getTable('locale_language')}` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC";
然后连接mysql数据库,执行这个语句:
mysql_connect("localhost","root","");
mysql_select_db("dbname");
mysql_query($sql);
就可以了。
注意连接数据库帐号的权限要有创建表的权限,否则创建就不会成功!
追问
这个php文件里有十几个建表语句呢,我都要依依这样做吗?我的意思是可不可以操作这个php文件,就把所有表都建好了
追答
这个省不了,你就依次:
mysql_query($sql1);
mysql_query($sql2);
mysql_query($sql3);
......
......
展开全部
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
mysql_select_db("database",$link);
mysql_query("set names utf-8");
mysql_query(sql语句);
mysql_close($link);
mysql_select_db("database",$link);
mysql_query("set names utf-8");
mysql_query(sql语句);
mysql_close($link);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询