Search code examples
phpmysql-connectmysql-select-db

"mysql_select_db" fails while "mysql_connect" OK


new here, only starting basic PHP. Trying to understand the specifics of the two MySQL commands. Please see this code

<?php   
$hostname = "localhost";
$username = "";
$password= "";
$databaseName = "alphacrm";

$dbConnected = mysql_connect($hostname, $username, $password);

$dbSelected = mysql_select_db($databaseName, $dbConnected);   ?>

If I run the code:

mysql_connect - successful

mysql_select_db - failed.

Question: why won't both fail if the $username is empty/wrong?

Note: I know I just need to input the value for $username and it will work. I am not trying to make it work, I am trying to understand why BOTH functions are not failing when $username has no value

Thanks.

Edit: @Jason: Thank you, I now know I am using outdated learning material


Solution

  • Try to get more information with

    if ($dbSelected){
     ...
    } else {
        die ('Can\'t use foo : ' . mysql_error());
    }
    

    it might tell you what the problem is!

    Put it in both of your else statements so you will know what mysql is doing!