-2

I am migrating from mysql to mysqli.Currently my server is running pdo, mysql and mysqli at the same time. Also the PHP 5.6.34 is running in a docker. MySQL version is 10.2.16-MariaDB-log MariaDB Server, which is running on the host machine. OS is CentOS Linux 7.

The script to connect to the db is:

  if($mysqli){//never works
    $res  = $GLOBALS["___mysqli_ston"] = mysqli_connect("localhost",  "user",  "pass" , NULL, 0, 'var/sockets/mysql.sock');
  }
  else{//always works
    $res  = mysql_connect(':/var/sockets/mysql.sock', "user", "pass" ); 
  }
  if (!$res){ 
    die("err");  
  }  

Connecting to db using mysql works fine. But every time I try to connect to db using mysqli get the following error:

( ! ) Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/Lib_SQL.php on line 20

Line 20:

$res = $GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "user", "pass" , NULL, 0, 'var/sockets/mysql.sock');

I've already set the default socket on php.ini file:

mysql.default_socket => /var/mysql/mysql.sock

I've also noticed that forcing a TCP/IP connection to the db does not cause this warning, For instance:

$res  = $GLOBALS["___mysqli_ston"] = mysqli_connect("hostname.com",  "user",  "pass" , NULL, 0, 'var/sockets/mysql.sock');

So how do I use mysqli_connect to connect to my db using a local unix socket? Any hints?

Ihidan
  • 419
  • 1
  • 6
  • 20

1 Answers1

-2

You are doing it the wrong way.

Try the code below. Change the database credentials to yours

<?php
         $dbhost = 'localhost';
         $dbuser = 'db username here';
         $dbpass = 'db -password here';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

         if(! $conn ){
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully';
         mysqli_close($conn);
      ?>
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38