CallumA Posted July 25, 2011 Share Posted July 25, 2011 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. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 http://php.net/manual/en/mysqli.connect.php Parameters host username passwd dbname port socket Quote Link to comment Share on other sites More sharing options...
CallumA Posted July 25, 2011 Author Share Posted July 25, 2011 Can I have some sample code please? I tried using that and it still couldn't connect. I know MySQL is working because phpMyAdmin connects to it via socket fine. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 mysqli($host, $user, $pass, $database, $port, $socket); If that doesn't work, what's the error message. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 25, 2011 Share Posted July 25, 2011 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 Quote Link to comment Share on other sites More sharing options...
CallumA Posted July 25, 2011 Author Share Posted July 25, 2011 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. Quote Link to comment Share on other sites More sharing options...
CallumA Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks teynon, it worked! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.