Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect using mysqli_connect, but mysql_connect works..?

Tags:

php

mysql

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?

like image 906
bboysupaman Avatar asked Dec 15 '22 19:12

bboysupaman


1 Answers

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.

like image 136
Bill Karwin Avatar answered Dec 17 '22 09:12

Bill Karwin