locoboy
locoboy

Reputation: 38960

Why isn't mysqli_connect working but mysql_connect is?

Are there any reasons why this might be the case?

Here's the code:

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
echo $dbc;
if(!$dbc){
die('could not open a connection'.mysqli_connect_errno() . mysqli_connect_error());
}

Again, if I replace mysqli with mysql I get a returned dbc resource id (I assume that's good). I would like to use mysqli as I hear it's much better/faster. Right now I'm getting error code 2003.

Upvotes: 3

Views: 7611

Answers (3)

leoap
leoap

Reputation: 1729

consider using

$dbc= new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

Upvotes: 0

Paul DelRe
Paul DelRe

Reputation: 4029

mysql_connect() and mysqli_connect() use two different default ports from the php.ini file. I wouldn't guess that these would be different than the standard default 3306, but worth a check or try adding the path to the host url.

mysqli_connect:

mysqli mysqli_connect ( ... int $port = ini_get("mysqli.default_port") ... )

mysql_connect:

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

Edit: I was wrong because I didn't read the manual page far enough.

I think your connection is fine, but you wouldn't want to check the object it returns as you have above. You'd want to use mysqli_connect_errno(). Below is the example from PHP.net.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

Note:
OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.

Source

Upvotes: 3

George Cummins
George Cummins

Reputation: 28936

mysqli_connect and mysql_connect take different parameters and return different values.

Upvotes: 1

Related Questions