gpt4 book ai didi

php - SQL/PHP 创建数据库并加载 .CSV

转载 作者:行者123 更新时间:2023-11-30 22:57:52 24 4
gpt4 key购买 nike

到目前为止,我已经删除了所有表并创建了数据库,并且一切正常。但是当我尝试创建一个新表并加载一个 .csv 文件时,它不起作用。

mysql_select_db($dbName, $dbConnection);
$sql = "CREATE TABLE ".$dbTable." (ID int(6) auto_increment NOT NULL, Username varchar(20) NOT NULL, Password varchar(20) NOT NULL, UserType int(1) NOT NULL, FirstName varchar(15) NOT NULL, LastName varchar(15) NOT NULL, DOB DATETIME NOT NULL, Phone varchar(15) NOT NULL, Department varchar(15),PRIMARY KEY(ID))";
if(mysql_query($sql, $dbConnection))
{
echo("Table '".$dbTable."' created<br />");
}
else
{
echo("Error creating table '".$dbTable."': ".mysql_error()."<br />");
}

$sql = "LOAD DATA INFILE 'users.csv' INTO ".$dbTable." FIELDS TERMINATED BY ',' (ID, Username, Password, UserType, FirstName, LastName, DOB, Phone, Department)";
if(mysql_query($sql, $dbConnection))
{
echo("Added records to '".$dbTable."' table<br />");
}
else
{
echo("Error inserting records into '".$dbTable."': ".mysql_error()."<br />");
}

最佳答案

你可以试试这个。我希望它会起作用

    LOAD DATA INFILE '<filePath>' 
INTO TABLE <tableName>
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, Username, Password, UserType, FirstName, LastName, DOB, Phone, Department);

关于php - SQL/PHP 创建数据库并加载 .CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com