0

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:

enter image description here

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

8
  • 1
    Are you using mysqli_connect first? Commented Jul 5, 2013 at 21:01
  • have you actually connected to the database before this line with mysqli_connect or new mysqli? Commented Jul 5, 2013 at 21:02
  • Yes, I think I should've added that code too Commented Jul 5, 2013 at 21:02
  • do you have the rights to access the database? what's the error message? Commented Jul 5, 2013 at 21:02
  • 1
    you can select the database by writing its name in the mysqli_connect() Commented Jul 5, 2013 at 21:09

2 Answers 2

4

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().

Sign up to request clarification or add additional context in comments.

4 Comments

Wow...this was actually it. Thank you, and sorry if the question was too obvious, I read so much about this and didn't find this...
Btw, you could select the db with the mysqli_connect() too.
@Big_Chair Just read the documentation linked to: it's the fourth parameter.
Added to the answer anyway (-:
1

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");

1 Comment

@Big_Chair, yes, noticed that, I guess I was typing the answer when complex857 submitted the answer. I don't want to delete this post for the documentation link :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.