2

I am writing a simple code to fetch 2 elements from a table in my database called Plasma. It seems the connection is being made well but I am unable to select any database due to unknown reason.

Code :

<?php
$db  = "Plasma"; 
$dbH = "localhost";
$dbU = "plasma";
$dbP = "plasma";
      $dbCon = mysqli_connect($dbH,$dbU,$dbP,$db);
        if(!dbCon){
            echo "Conenction Fail";
        }
        mysqli_select_db($dbCon,$db);
        $qry = "select Mid,Mname from ya_movies order by DOA limit 5;";
        $Response = mysqli_query($dbCon,$qry);
        echo mysqli_error($dbCon);                     ?>

The Output is

No database selected
BMC
  • 591
  • 3
  • 7
  • 22

5 Answers5

11

It's a typical case of not enough debug information:

mysqli_select_db($dbCon, $db);

You don't check for the return value; it may return false to indicate it couldn't change the database (probably due to permissions). I would suggest putting this in place and run it:

if (!mysqli_select_db($dbCon, $db)) {
    die("Uh oh, couldn't select database $db");
}

If this happens, double check the name, permissions, etc.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

I can't comment, I dont have the reputation yet, but:

if(!$dbCon){
    echo "Connection Failure!";
}

try this:

if (!$dbCon) {
    die('Connection Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

Might give a more meaningful error message.

Lux
  • 1,540
  • 1
  • 22
  • 28
EvilEpidemic
  • 479
  • 8
  • 17
1

PHP says “No Database selected” even after using mysqli_select_db()

Nope, it doesn't, if you are using valid credentials.

PHP will set current database all right either way.
The only reason for not to do so is a some sort of typo you made.

You just need to be sure that you actually called this function with proper database name.
And then double-check it. Of course a user have to have rights to access that database.

That's all.
There is no magic.
There is no bug in mysqli.
There is but only one rational explanation. Always trust to your common sense.

A couple of rules you need to learn to avoid such a problem in a future:

  1. In all your further codes ALWAYS make error_reporting(E_ALL); the very first line and never turn it off nor make it less. This way you will be notified of the errors occurred in your code.
  2. Before using database credentials in the code, test them in a console. This way you will make sure that your credentials are proper and works. If have problem with setting proper credentials - as k a question on setting them.
  3. If there are no errors and credentials are all proper - check for the typos in the code.
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0
if(!dbCon){
   echo "Conenction Fail"; 
}

You forgot to add $ in dbCon variable.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
-1

I had the same problem. I created a new database but it could not be selected from PHP. Yet the test database could be selected. I solved it by changing the $user to 'root'. For your case :

$dbU = 'root'

That should work for you.

TechWisdom
  • 3,960
  • 4
  • 33
  • 40
  • This may solve the problem. But a better solution would be to configure the database so it worked with the user specified. – Joe Flynn Jul 11 '15 at 11:56