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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.