問題描述
如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)
I was running an example from a mysqli tutorial.
$mysqli = new mysqli("localhost", "user", "password", "database");
if($mysqli‑>connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli‑>connect_errno . ) "
. $mysqli‑>connect_error;} echo $mysqli‑>host_info . "\n";
}
I was getting the error:
Warning: mysqli::mysqli(): (42000/1049): Unknown database 'world' in /home/...
What should I do to create the database?
‑‑‑‑‑
參考解法
方法 1:
You have to use this:
$mysqli=new mysqli("localhost","user","password") or die(".........");
if($mysqli‑>query("Create database if not exists MyDB")){
"Failed creating new database: ".$mysqli‑>connect_errno();
die();
}
$mysqli‑>select_db("MyDB");
$mysqli‑>close();
方法 2:
try something like this
$mysqli = new mysqli("localhost", "user", "password");
in this way you are connected to database
and put this query using mysqli
Create Database database1
(by user1421176、Francisco、maxjackie)