0
我目前正在研究PHP/MySQL排名系統,但是我的CREATE TABLE
聲明出現問題。MySQL沒有通過PHP創建表格
這裏是我的代碼:
mysql_select_db("DB1");
$numrows = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'DB1'";
if($numrows > 1){
if($numrows > 2){
$table = rand(1, $numrows);
}else{
$table = rand(1, 2);
}
}else{
$table = rand(1, 1);
}
$checkForTable = mysql_query("SELECT 1 FROM $table LIMIT 1");
if($checkForTable){
$query = "INSERT INTO $table (name,score) VALUES($name, $score)";
$result = mysql_query($query);
mysql_select_db("DB2");
$query2 = "INSERT INTO Leaderboard (name,score) VALUES($name, $score)";
$result2 = mysql_query($query2);
mysql_select_db("DB1");
if($result && $result2){
echo "<h4 style='color:green;'>Your Score Has Been Inserted</h4><hr/>";
}else{
echo "<h4 style='color:red;'>We encountered an error while inserting your data </h4><hr/>";
}
}else{
$newtable = "CREATE TABLE $table (
id bigint AUTO_INCREMENT NOT NULL,
name varchar(255) NOT NULL,
score bigint(20) NOT NULL,
PRIMARY KEY('id')
)";
$result = mysql_query($newtable);
if($result){
$query = "INSERT INTO $newtable (name,score) VALUES($name,$score)";
$result = mysql_query($query);
mysql_select_db("DB2");
$query2 = "INSERT INTO Leaderboard (name,score) VALUES($name, $score)";
$result2 = mysql_query($query2);
mysql_select_db("DB1");
if($result && $result2){
echo "<h4 style='color:green;'>Your Score Has Been Inserted</h4><hr/>";
}else{
echo "<h4 style='color:red;'>We encountered an error while inserting your data </h4><hr/>";
}
}else{
echo "TableNotCreatedException: " . mysql_error();
}
}
當我嘗試代碼我得到:
您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的正確語法手冊「1(ID BIGINT AUTO_INCREMENT NOT NULL,名稱VARCHA」在行1
我一直在試圖算出這個一會兒但我沒有運氣。請幫助!
@ R.Harley,有你仔細紅答案。錯誤是因爲您使用的數字作爲表名不可接受。嘗試使用不同的名稱。閱讀文件。 – Rahul
好吧@Rahul,我得到了一些與表存在的錯誤,所以我把它改爲CREATE TABLE IF NOT EXIST $ table'(...)'現在我得到錯誤你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在'EXIST 1'附近使用正確的語法(id bigint AUTO_INCREMENT NOT NULL,nam'at line 1' –
我已將CREATE TABLE $ table更改爲CREATE TABLE' $ table' –