0
我對PDO和MySQL相當陌生,並且遇到了一個奇怪的問題。我查看了很多PDO和MySQL線程,但沒有人似乎有這個確切的問題。通過PHP PDO創建MySQL表後無法插入
當我創建數據庫,新的mysql新用戶,一個表和幾列後,我無法使用INSERT INTO來填充數據庫。 PDO引發以下錯誤:
SQLSTATE [42S02]:基表或視圖未找到:1146表「cms01db.cms_settings」不存在
如果我看在phpMyAdmin,數據庫中存在,該表和列存在,排序正確等。如果我然後在PHPMyAdmin中刪除表並在那裏手動重新創建它,具有完全相同的規格,我可以使用相同的INSERT INTO腳本,它工作得很好。
我在做什麼錯?
腳本來創建數據庫:
// define server and root login
$servername = 'localhost';
$username = 'root';
// define new user for new database and login password
$newUser = 'admin';
$newPass = 'password';
$dbname = 'cms01db';
// create database
try {
// connect to mysql and login
$conn = new PDO("mysql:host=$servername", $username);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// setup command to create database, new user and asign to new database
$sql = "CREATE DATABASE `$dbname` CHARACTER SET ascii COLLATE ascii_general_ci;
CREATE USER '$newUser'@'localhost' IDENTIFIED BY '$newPass';
GRANT ALL ON `$dbname`.* TO '$newUser'@'localhost';
FLUSH PRIVILEGES;";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database created successfully<br>";
}
// if shit goes wrong...
catch(PDOException $e)
{
// display error why
echo $sql . "<br>" . $e->getMessage();
}
// terminate db connection
$conn = null;
腳本來創建表和列:
// login with new user and create database tables & columns
try {
// connect to mysql and login
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $newUser, $newPass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table and columns
$sql = "DROP TABLE IF EXISTS cms_setings;
CREATE TABLE cms_setings (
measurements VARCHAR(1) NOT NULL,
brasizes VARCHAR(3) NOT NULL,
shoesizes VARCHAR(3) NOT NULL
)";
// use exec() because no results are returned
$conn->exec($sql);
echo "CMS Settings Table created successfully<br>";
}
// if shit goes wrong...
catch(PDOException $e)
{
// display error why
echo $sql . "<br>" . $e->getMessage();
}
// terminate db connection
$conn = null;
腳本插入到:
//prepare variables
//measurements: 'm' for metric, 'i' for inches
$measurements_data = 'i';
//brasizes: 'us' for USA/Canada, 'uk' for UK, 'aus' for Australia, 'eu' for Europe, 'bef' for Belgium/Spain/France
$brasizes_data = 'us';
//shoesizes: 'us' for USA/Canada, 'uk' for UK, 'eu' for Europe, 'jap' for Japan
$shoesizes_data = 'us';
// login and insert data into columns
try {
// connect to mysql and login
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $newUser, $newPass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create insert data
$sql = "INSERT INTO cms_settings (measurements,brasizes,shoesizes) VALUES (:measurements,:brasizes,:shoesizes)";
//prepare the statement
$stmt = $conn->prepare($sql);
// execute statement
$stmt->execute(array(':measurements' => $measurements_data,
':brasizes' => $brasizes_data,
':shoesizes' => $shoesizes_data));
echo "CMS Setting Populated Successfully<br>";
}
// if shit goes wrong...
catch(PDOException $e)
{
// display error why
echo $sql . "<br>Error: " . $e->getMessage();
}
// terminate db connection
$conn = null;
似乎是一個錯字:'cms_setings',而不是'cms_settings'。 – Federkun
@Leggendario - 你打我吧:D – Jesse
親愛的主,現在我覺得很蠢哈哈。非常感謝!現在完美的工作^ _ ^令人難以置信的是,你可以在幾小時和幾小時盯着相同的代碼時發現這樣的微小錯誤... – Insomniaboy