我正在努力使我的代码检查以查看数据库具有的表数然后增加它然后将数据库添加到数据库中,但是,我收到以下错误?任何帮助都会非常感激。
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jobs";
// Create connection
$link = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($link, "show tables");
(int)$num = 0;
while($table = mysqli_fetch_array($result)) {
$num++;
echo($table[0] . "<BR>");
}
$num += 1;
$sql = "CREATE TABLE {$num} (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";
if(!mysqli_query($link, $sql)){
echo("Error description: " . mysqli_error($link)); }
输出错误
错误说明:SQL语法中有错误;检查与MariaDB服务器版本对应的手册,以便在#5;附近使用正确的语法(id INT(6)UNSIGNED AUTO_INCREMENT PRIMARY KEY,firstname VARCHAR(30)N&#39; at line 1
答案 0 :(得分:0)
输入默认表名,后跟num,如此
CREATE TABLE table{$num} (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";
您不能将号码作为表名
答案 1 :(得分:0)
标识符可以以数字开头,但除非引用可能不仅仅由数字组成。
来源:https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
为了使用tablename的数字,你应该将{$ num}变量包装在单引号中,如
$sql = "CREATE TABLE `{$num}`(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";