Domanda

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

È stato utile?

Soluzione

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!

Altri suggerimenti

mysql_connect is enclosed in a variable $dbConnected, it is nothing to do until unless it has been put in mysql_connect.

when PHP reads the mysql_connect it finds the incorrect credentials so that's wht mysql_connect fails.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top