Xamp,PHP,Mysql

时间:2017-10-20 05:30:27

标签: php mysql

我想使用php在MySQL数据库的“帐户”中添加一个新表“user2”。 以下脚本将运行,没有错误,但在MySQL中没有创建任何新表。

<?php

    $accounts = mysqli_connect("localhost","root","2277") 
    or die(mysqli_error());

    mysqli_select_db($accounts, "accounts");

    $sql = "CREATE TABLE users2
    {
        ID int NOT NULL AUTO_INCREMENT,
        PRIMERY KEY (ID),
        Username varchar(20),
        Password varchar (20),
        First varchar (20),
        Last varchr (20)
    }
    ";

    mysqli_query($accounts, $sql);


?>

3 个答案:

答案 0 :(得分:0)

将您的查询更改为:

CREATE TABLE `users2` (
    `ID` INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (ID),
    `Username` VARCHAR(20),
    `Password` VARCHAR(20),
    `First` VARCHAR(20),
    `Last` VARCHAR(20)
)

你有一些错字,不要粗鲁但是我建议你先在这里发表之前检查一下这些单词,以及如何正确地做一个CREATE TABLE,这很容易用Google搜索:

https://www.w3schools.com/sql/sql_create_table.asp

https://www.tutorialspoint.com/sql/sql-create-table.htm

https://www.techonthenet.com/sql/tables/create_table.php

答案 1 :(得分:0)

我修复了你的SQL语句。看看这个:

<?php

    $accounts = mysqli_connect("localhost","root","2277") 
    or die(mysqli_error());

    mysqli_select_db($accounts, "accounts");

    $sql = "CREATE TABLE users2
    (
        ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
        Username varchar(20),
        Password varchar (20),
        First varchar (20),
        Last varchar (20)
    )
    ";

    mysqli_query($accounts, $sql);


?>

答案 2 :(得分:0)

试试这个,它适用于我

<?php

    $accounts = mysqli_connect("localhost","root","") or die(mysqli_error());

    mysqli_select_db($accounts, "accounts");

    $sql = "CREATE TABLE IF NOT EXISTS `user2` ( `ID` INT NOT NULL AUTO_INCREMENT , `Username` VARCHAR(20) NOT NULL , `Password` VARCHAR(20) NOT NULL , `First` VARCHAR(20) NOT NULL , `Last` VARCHAR(20) NOT NULL , PRIMARY KEY (`ID`))
    ";

    $query = mysqli_query($accounts, $sql);

    if ($query == true) {
        echo "Success";
    }else{
        echo "Failed";
    }

?>