用PHP在MySQL中创建表的问题

时间:2018-10-05 03:45:27

标签: php mysql

我想使用之前创建的schema.sql文件在mysql中创建表,但是php创建第一个表并在下一个表中给我错误,尽管sql命令没有问题

php代码:

<?php
$host = "localhost";
$username = "root";
$password = "pass";
$dbname = "test";
$content = file_get_contents("schema.sql");
$ms = new mysqli($host,$username,$password,$dbname);
if($ms->connect_error){
    die("con failed : ".$ms->connect_error);
}
if($ms->query($content) === true){
    echo "ok"."\n";
}else{
    echo $ms->error . "\n";
}

schema.sql:

create table users (
  id int primary key auto_increment,
  username varchar(255) not null unique ,
  password varchar(255) not null ,
  email varchar (255) not null unique ,
  time_created datetime not null
);


create table todos (
  id int primary key auto_increment,
  user_id int not null,
  title varchar (255) not null ,
  content text ,
  time_created datetime not null ,
  constraint foreign key (user_id) references users(id)
);

,然后给我这个错误:

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 todos (
  id int primary key auto_increment,
  user_id int not null' at line 10

0 个答案:

没有答案