简体   繁体   中英

PHP - Selecting database from MySQL doesn't work

I have read a lot about this but it still doesn't work.
I'm just trying to select a database to create a new table in, I try:

$db = mysqli_select_db("test");
if(!$db) {
echo "error: " . mysqli_error($db);
}

But I still get an error (and mysqli_error($db) doesn't seem to work).

Of course I have already connected to it:

$con=mysqli_connect("localhost", "administrator", "****");



On phpMyAdmin I have these databases:

在此处输入图片说明

Why can't I select "test" ?
And creating a database doesn't work because I don't have the rights, as you can see.

The procedular signature of this function is:

 bool mysqli_select_db ( mysqli $link , string $dbname )

So you will have to provide the resource you got back from the mysqli_connect() to make it work. Something like this:

$con = mysqli_connect("localhost", "administrator", "****");
$success = mysqli_select_db($con, "test");

Alternatively you could specify the database on the connect call with a 4th argument:

$con = mysqli_connect("localhost", "administrator", "***", "test");

See the examples on mysqli_connect() .

mysqli_select_db function requires two parameters link and dbname . Please refer to the documentation:

http://php.net/manual/en/mysqli.select-db.php

You are only passing link and no database name in your call:

$db = mysqli_select_db("test");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM