我是PDP PDO的新手,这是我创建名为" title"
的数据库的代码<?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'
有什么问题?
答案 0 :(得分:0)
您连接到此处未出现的数据库:
func addAlert()
{
alert.addTextField(configurationHandler: { (textField) -> Void in
textField.placeholder = "Enter Name"})
//grab value from the text field and print when user clicks OK
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) in
let textField = self.alert.textFields![0] as UITextField
print("Text Field: \(textField.text!)")
self.addNote()
}))
self.present(alert, animated: true, completion: nil)
}
删除dbname:
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
答案 1 :(得分:0)
您在第7行遇到SQL错误,因为您正在尝试访问尚不存在的myDB
并且正在抛出异常。
您收到未定义的变量错误,因为try
语句中的代码在$sql
被分配给任何内容之前失败。