allen
allen

Reputation: 1

How do I get mysqli_connect() to work?

While running this simple script:

<?php
$link = mysqli_connect("localhost", "*****", "********", "***");
if (!$link) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
  }
printf("Host information: %s\n", mysqli_get_host_info($link));
mysqli_close($link);
?>

on my networksolutions account I received this error

Connect failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I called network solutins tech support twice. They "re synchronized" the server and we reverified the "connection strings" the function parameters...but no go. I tried switching from mysql_connect() to mysqli_connect().

I don't know what it could be. I know there must by thousands of NS customers who do this but did not find an answer on their forum.

Please help. Anyone interested in working for equity in my website please let me know as well.

Thanks

Upvotes: 0

Views: 2439

Answers (5)

jake
jake

Reputation: 185

Try using PDO used this example as your basis

    <?php
     $orig = $_GET['orig'];
     $des_id = $_GET['des_id'];
            try {
                $dbuser = "kim";
                $dbpass = "kim";
    $conn = new PDO('mysql:host=localhost;dbname=destination', $dbuser, $dbpass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
    $stmt = $conn->prepare("SELECT pl_id FROM view_places WHERE name = :name LIMIT 1");

                $stmt->bindParam(':name',$orig); 
                $stmt->execute();
                $result_1 = $stmt -> fetch();
                $res1 = $result_1["pl_id"];  

                $stmt->bindParam(':name', $des_id); 
                $stmt->execute(); 
                $result_2 = $stmt -> fetch(); 
                    $res2 = $result_2["pl_id"];  
                    echo   'origin_number:'.$res1. ', '.'destination_id:'.$res2;
                }   catch(PDOException $e) {
                        echo 'ERROR: ' . $e->getMessage();
                }

            ?>

Upvotes: 1

Learner
Learner

Reputation: 13

Below code may be help!

<?php
/*SERVER database connection*/
    function dbconnect(){
    $connection = mysql_connect('localhost','****','****);
        if(!$connection){
        return false;
        }
        if(!mysql_select_db('database_name')){
        return false;
        }
        return $connection;
    }
?>

Upvotes: 1

Pekka
Pekka

Reputation: 449783

Try 127.0.0.1 instead of localhost to make the connection using TCP/IP rather than through the local socket, which seems to be broken for one reason or another.

Background

Upvotes: 1

Xorlev
Xorlev

Reputation: 8653

Let them know that they not only need to set the proper mysql plugin default socket, but the mysqli plugin default socket.

From my server's php.ini config:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket  = "/var/lib/mysql/mysql.sock"
mysqli.default_socket = "/var/lib/mysql/mysql.sock"

Thus both must be set. I bet they didn't set mysqli.default_socket.

Upvotes: 0

Christian Mann
Christian Mann

Reputation: 8125

Are you sure it should be localhost? I know that Dreamhost does not allow localhost connections to go through -- you have to connect to mysql.example.com.

Upvotes: 0

Related Questions