how can I creates a mysqli database for first the

2019-02-27 01:06发布

问题:

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



标签: php mysqli