Can't connect with mysqli_connect but mysql_connect works ..?

I got my MySQL connection credentials directly from my hosting provider (1 and 1). However, when I try to connect with the following code:   

$db_hostname = 'localhost/tmp/mysql5.sock';
$db_database = 'db543062602';
$db_username = '***********';
$db_password = '***********';

$dbc = mysqli_connect($db_hostname, $db_username, $db_password, $db_database)
    or die('Error connecting to MySQL server');



Warning: mysqli_connect (): (HY000 / 2005): Unknown MySQL server 'localhost / tmp / mysql5.sock' (1) in /homepages/........./purge.php on line 10 Error connections to MySQL server

The credentials are all the same. In fact, it works when connected with mysql_connect (), but it doesn't want to work with mysql_connect ()! Any thoughts on what might be causing the problem?


source to share

4 answers

Either you misinterpreted instructions from 1 and 1, or they made a mistake.

Read the man page for mysqli_connect () . It indicates that the first argument must be the hostname, either by name or by IP. Do not include socket in this argument.

You can supply a socket file as the sixth argument.

/* WRONG */
mysqli_connect('localhost/tmp/mysql5.sock', ... );

/* WRONG */
mysqli_connect('localhost:/tmp/mysql5.sock', ... );

/* RIGHT */
mysqli_connect('localhost', $db_username, $db_password, $db_database, null, '/tmp/mysql5.sock'); 


Also, I'm wondering why they even require you to specify the socket. They should define a default in the host's php.ini file, so you don't need to. Unless maybe they are not starting multiple instances of mysqld on the same host.



A quick look at the mysqli_connect () page :

mysqli mysqli_connect ([string $ host = ini_get ("mysqli.default_host") [, string $ username = ini_get ("mysqli.default_user") [, string $ passwd = ini_get ("mysqli.default_pw") [, string $ dbname = "" [, int $ port = ini_get ("mysqli.default_port") [, string $ socket = ini_get ("mysqli.default_socket")]]]]]])

... and:


Can be either hostname , or an IP address . Passing NULL or the string "localhost" for this parameter, assuming local host. Pipes will be used instead of TCP / IP when possible.

power socket

Specifies the socket or named pipe to be used.

I'll admit the man page might use an example, but there is no trace of the syntax you are trying to follow.

Remember mysqli is a different extension than the legacy mysql:

resource mysql_connect ([string $ server = ini_get ("mysql.default_host") [, string $ username = ini_get ("mysql.default_user") [, string $ password = ini_get ("mysql.default_password") [, bool $ new_link = false [, int $ client_flags = 0]]]]])

... where:


MySQL server. It can also contain a port number. eg "hostname: port" or local socket path for example. ": / path / to / socket" for localhost.



I think your hostname should be:



and not:



Or it looks like there is also an alternative method to find the host so that you try to get data from your panel.



just add mysql.default_socket = / tmp / mysql5.sock; this line in php.ini at the root of your server.



All Articles