使用PHP创建多个MySQL表 [英] Create Multiple MySQL tables using PHP

查看:86
本文介绍了使用PHP创建多个MySQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用php创建一个以上的MySQL表,但它给出了错误,而且我无法弄清它的问题所在.

I am trying to create more then one MySQL tables using php but it gives errors and i can not sort out what is wrong with it.

PHP代码:

$sql = ' CREATE TABLE IF NOT EXISTS `mod_reminder_entries` 
  ( 
   `id`          INT(10) NOT NULL auto_increment, 
   `user_id`     INT(10) NOT NULL, 
   `entry_name`  VARCHAR(255) NOT NULL, 
   `entry_value` INT(10) NOT NULL, 
   PRIMARY KEY (`id`), 
   FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
 ); 

 CREATE TABLE IF NOT EXISTS `second_table` 
  ( 
   `user_id`     INT(10) NOT NULL, 
   `fieldstotal` INT(10) NOT NULL, 
   FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
  ); ';

  mysql_query($sql);

它给出了错误,另外不用担心mysql连接.我已经正确连接到数据库,并且已经对其进行了测试,语法肯定是错误的.

It gives the error, besides do not worry about the mysql connection. I properly connect with the DB and i have tested it, it is definitely some thing wrong with syntax.

您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本以使用正确的语法 如果不存在,则创建表second_table(user_id INT

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 'CREATE TABLE IF NOT EXISTS second_table ( user_id INT

推荐答案

您不能将分号分隔的查询与 mysql_query ,此功能一次只允许一个查询!

You cannot use semicolon separated queries with mysql_query, this function only allows one query at a time!

您必须分别执行语句:

mysql_query("
    CREATE TABLE IF NOT EXISTS `mod_reminder_entries` (
        `id` INT(10) NOT NULL AUTO_INCREMENT, 
        `user_id` INT(10) NOT NULL, 
        `entry_name` VARCHAR(255) NOT NULL, 
        `entry_value` INT(10) NOT NULL, 
        PRIMARY KEY (`id`), 
        FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
    )
") or die(mysql_error());

mysql_query("
CREATE TABLE IF NOT EXISTS `second_table` (
    `user_id` INT(10) NOT NULL, 
    `fieldstotal` INT(10) NOT NULL, 
    FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
)
") or die(mysql_error());

或者更好,使用 mysqli_multi_query -这意味着您必须进行切换到 mysqli .

Or better, use mysqli_multi_query -- which means you must switch to mysqli.

这篇关于使用PHP创建多个MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆