I got the credentials for connecting to MySQL directly from my hosting provider (1and1). 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');
?>
ERROR:
Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'localhost/tmp/mysql5.sock' (1) in /homepages/... ... ... /purge.php on line 10 Error connecting to MySQL server
The credentials are all the same. In fact, it works when connecting using mysql_connect(), but it doesn't want to work with mysql_connect()! Any thoughts on what could be causing the problem?
Either you have misinterpreted the instructions from 1&1, or else they made a mistake.
Read the manual page for mysqli_connect(). It shows that the first argument should be the hostname, either by name or IP. Do not include the socket in that argument.
You can optionally specify the 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 wonder 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 have to. Unless perhaps they're running multiple instances of mysqld on the same host.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With