我想要的只是通過 PHP 使用文件創(chuàng)建 2 個或更多表tables.sql。PHPcreate_db.php<?phprequire 'config.php';$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);/* check connection */if($mysqli->connect_errno){ echo "MySQL connection failed.<br>"; exit();}else{ echo "MySQL successfully connected.<br>";}// DB createif($mysqli->query('CREATE DATABASE IF NOT EXISTS '.DB_NAME.';') === TRUE){ echo "Database successfully created.<br>";}else{ echo "Error: ".$mysqli->errno.", ".$mysqli->error."<br>";}// DB selectif($mysqli->select_db(DB_NAME) === TRUE){ echo "Database successfully selected.<br>";}else{ echo "Error: ".$mysqli->errno.", ".$mysqli->error;}// Create tablesif($mysqli->query(file_get_contents('../sql/tables.sql')) === TRUE){ echo "Tables successfully created.<br>";}else{ echo "Error: ".$mysqli->errno.", ".$mysqli->error."<br>";}$mysqli->close();?>DB_HOST、DB_USER、DB_PASSWORD、DB_NAME 在 config.php 文件中定義PHP v7.4.7SQLtables.sqlCREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL auto_increment, `username` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`));CREATE TABLE IF NOT EXISTS `status` ( `status` varchar(100) NOT NULL, `IDtime` varchar(100) NOT NULL);如果我使用命令行將此tables.sql 文件直接上傳到MySQL 服務(wù)器,它就可以工作。MySQL v8.0.20 MySQL 社區(qū)服務(wù)器 - GPL錯誤信息錯誤:1064,您的 SQL 語法有錯誤;檢查與您的 MySQL 服務(wù)器版本相對應(yīng)的手冊,了解在第 16 行的'CREATE TABLE IF NOT EXISTS status( varchar(100) NOT NULL, `IDt' 附近使用的正確語法status
1 回答

長風(fēng)秋雁
TA貢獻1757條經(jīng)驗 獲得超7個贊
例子:
$result = $mysqli->query("SELECT * FROM reservations;SELECT * FROM reservations");
var_dump($result, $mysqli->error);
bool(false)
string(172) "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 'SELECT * FROM reservations' at line 1"
$result = $mysqli->multi_query("SELECT * FROM reservations;SELECT * FROM reservations");
var_dump($result, $mysqli->error);
bool(true)
string(0) ""
- 1 回答
- 0 關(guān)注
- 122 瀏覽
添加回答
舉報
0/150
提交
取消