Jump to content

Using PHP and mysqli to connect to a mysql database using socket


CallumA

Recommended Posts

I need to access my mysql database using mysqli using a socket like phpMyAdmin does.

 

How would I do this?

 

This is my connect code:

$mysqli = new mysqli($server, $username, $password, $database);

 

Where do I put the socket? Is it in place of $server? If I do the socket URL there (/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock) the page load just hangs for a long time then comes out with the error "Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (60) in /Library/WebServer/htdocs/index.php on line 18".

 

Thanks in advance.

You don't need to use a socket to connect to MySQL. Just set the host field (first argument) to 'localhost'. Enter your mysql username into the second argument. Place the password for the user in the third argument. Last enter the mysql database you want to work with in the forth argument.

 

Example PHP code to connect to mysql using MySQLi

$mysqli = new mysqli('localhost', 'username', 'password', 'database');

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

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();

 

Documentation on mysqli connect

Thanks, I will try that sample code when I get home but I think it looks a lot like what I tried. Also, I am using socket because I have networking disabled in the mysql configuration for security and I have read somewhere that socket is faster anyway.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.