PHP MYSQL无法连接(HY000 / 2002)

时间:2012-07-08 01:58:17

标签: php mysql

我正在一个本地服务器(WAMP)的网站上工作,php和数据库工作正常。但是当我将网站上传到托管服务器。并尝试登录,数据库连接无法正常工作。当我使用像mysqli::mysqli()这样的数据库函数时,它会给我警告。

整个错误如下:

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): No connection could be made
because the target machine actively refused it. 
in D:\Hosting\XXXXX\html\raw\database\connect.php on line 9

继续

Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No connection could be made because
the target machine actively (trying to connect via tcp://localhost:3306) in     
D:\Hosting\XXXXX\html\raw\database\connect.php on line 9


Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in    
D:\Hosting\XXXXX\html\raw\functions\users.php on line 10

connect.php的php代码是:

<?php

    require_once 'Internal/Var.php';
    //mysql_connect(D_SERVER, D_USER, D_PASSWORD);
    //mysql_select_db(D_NAME);

    function create_connection()
    {
        $conn = new mysqli(D_SERVER, D_USER, D_PASSWORD, D_NAME);
        return $conn;
    }

    function close_connection(mysqli $con)
    {
        $con->close();
    }

?>

1 个答案:

答案 0 :(得分:1)

您需要为username连接指定passwordMySQLi

require_once 'Internal/Var.php';
// Remove the two procedural connection lines here

function create_connection()
{
    // Make sure D_SERVER, D_USER, D_PASSWORD, D_NAME have the correct values
    // These values are probably created in Internal/Var.php
    $conn = new mysqli(D_SERVER, D_USER, D_PASSWORD, D_NAME);

    return $conn;
}

function close_connection(mysqli $con)
{
    $con->close();
}