how can I creates a mysqli database for first the

2019-02-27 01:01发布

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?

标签: php mysqli
2条回答
贪生不怕死
2楼-- · 2019-02-27 01:41

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();
查看更多
Luminary・发光体
3楼-- · 2019-02-27 02:04

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

查看更多
登录 后发表回答