繁体   English   中英

使用PHP在MySQL中创建表时出错

[英]Error creating table in MySQL using PHP

我正在编写一个PHP脚本,以便创建一个Database ,然后使用它来创建一个Table 我假设我使用的是正确的MySQL语法,但是却遇到了这个错误- Error creating table: 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 'USE website; CREATE TABLE users ( id INT(11) UNSIGNED AUTO_INCREMENT PRI' at line 2 Error creating table: 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 'USE website; CREATE TABLE users ( id INT(11) UNSIGNED AUTO_INCREMENT PRI' at line 2

这是我的PHP脚本create_db.php

<?php

function create_database($hostname, $username, $password) {

$h = $hostname;
$u = $username;
$p = $password;

$conn = new mysqli($h, $u, $p);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "CREATE DATABASE website;
        USE website;
        CREATE TABLE users (
        id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(255) NOT NULL,
        lastname VARCHAR(255) NOT NULL,
        email VARCHAR(255) NOT NULL,
        password VARCHAR(255) NOT NULL,
        hash VARCHAR(255) NOT NULL,
        forgot_password_hash VARCHAR(255) NOT NULL,
        status ENUM('1', '0') NOT NULL
        );";

if ($conn->query($sql) === TRUE) {
echo "Table users created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
}

create_database('localhost', 'root', 'root');

?>

您必须使用multi_query()而不是query()

$conn->multi_query($query)

在此处查看有关multi_query更多详细信息: http : multi_query

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM