Can't connect to local MySQL server through socket

Since the error is being thrown by the call to mysql_real_escape_string() it rather implies that you didn't call mysql_connect() first and pass a valid db handle to mysql_real_escape_string (or the call to mysql_connect() failed).

In some circumstances, the mysql extension will attempt to connect automatically using the default settings in php.ini, failing over to my.cnf if these are not available - which obviously are not valid. Or it may be that the settings are valid but the mysqld is not running.

Have you got scripts which are connecting to the database successfully?

Do you have a username and password for the database?

Try:

check_running();

$user=''; // fill in your details
$password=''; // fill in your details

$hosts=array(
  'localhost', '127.0.0.1', $_SERVER['HTTP_HOST'], $_SERVER['SERVER_ADDR']
);
foreach ($hosts as $addr) {
   try_con($addr, $user, $password);
   try_con($addr . ':3306', $user, $password);
}

function try_con($host, $user, $password)
{
 $dbh=mysql_connect($host, $user, $password);
 if ($dbh) {
     print "Connected OK with $host, $user, $password<br />\n";
 } else {
     print "Failed with $host, $user, $password<br />\n";
 }
}

function check_running()
{
   // this assumes that you are using Apache on a Unix/Linux box
   $chk=`ps -ef | grep httpd | grep -v grep`;
   if ($chk) {
      print "Checking for mysqld process: " . `ps -ef | grep mysqld | grep -v grep` . "<br />\n";
   } else {
       print "Cannot check mysqld process<br />\n";
   }
 }

Yes exactly, some servers pass the default connection parameters when using mysql functions before connection and throw off an error, some other servers work just fine wherever you place the code it is always safer to just place mysql_real_escape_string() after establishing mysql_connect() connection

Tags:

Mysql

Sockets