尝试连接到数据库时,我收到以下MySQLI错误:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /nfs/c10/h03/mnt/144844/domains/trash.mysite.com/html/functions.php on line 43Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

我的连接文件:

<?php $host = 'internal-db.s144844.gridserver.com'; $user = 'db144844_db'; $password = '***********'; $db = 'db144844_db';?>

功能文件:

`

require('connection.php');

function sendMessage($message)
{
    // this line loads the library
    require('twilio-php-master/Services/Twilio.php');

    $account_sid = '***';
    $auth_token = '***';
    $client = new Services_Twilio($account_sid, $auth_token);

    $client->account->messages->create(array(
        'To' => "5555555555",
        'From' => "+6666666666",
        'Body' => $message,

    ));
}

function resetTrash()
{
    //this could be set to just go on a certain day like tuesday
    $connection=mysqli_connect($host , $user , $password , $db);

    //turn alerts off in the db cause we missed it or whatever
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query($connection,"UPDATE trash SET done=0 WHERE id=1");

    mysqli_close($connection);

}

function markDone()
{
    //this could be set to just go on a certain day like tuesday
    $connection=mysqli_connect($host , $user , $password , $db);

    //turn alerts off in the db cause we missed it or whatever
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query($connection,"UPDATE trash SET done=1 WHERE id=1");

    mysqli_close($connection);

}


?>`

这是第二个函数的第43行:

$connection=mysqli_connect($host , $user , $password , $db);

我的主机MediaTemple说,他们可以使用我的连接信息从侧面进行正常连接,但是套接字错误似乎与MySQL而不是我的代码有关。

我可以使用phpMyAdmin等登录数据库。

最佳答案

您只是忘记了函数参数:

function resetTrash(){
    $connection=mysqli_connect($host , $user , $password , $db);
                               ^^^^^   ^^^^^   ^^^^^^^^^   ^^^
                                 ?       ?         ?        ?


你要:

function resetTrash($host, $user, $password, $db){


PHP可以警告您有关此错误的信息。请阅读有关error reporting的信息。

10-07 12:51
查看更多