我是新來的PDP PDO,這是我的代碼來創建一個名爲「標題」錯誤,而試圖用PHP PDO創建數據庫
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE title";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database created successfully<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage(); // THIS IS LINE NUMBER 17
}
$conn = null;
?>
數據庫但這是錯誤的,我得到的,而我嘗試運行該腳本
Notice: Undefined variable: sql in C:\xampp\htdocs\website\database\createdb.php on line 17
SQLSTATE[HY000] [1049] Unknown database 'myDB'
出了什麼問題?
move'$ sql =「CREATE DATABASE title」;'to above'try {' – bansi
@Epodax對不起,它是'myDB'不是電影。我編輯了這個問題。 –
我從w3schools得到了這段代碼,它們不應該有任何錯誤。 –