本文介绍了PHP MYSQL无法连接(HY000/2002)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当时在本地服务器(WAMP)上的网站上工作,php和数据库工作正常.但是当我将网站上传到托管服务器时.并尝试登录,则数据库连接将无法正常工作.当我使用mysqli::mysqli()
之类的数据库功能时,它会向我发出警告.
i was working on a website on a local server (WAMP) and php and database worked fine. but when i uploaded the site to the hosting server. and tried to login, the DB connection would not work. it gives me warnings when i use DB functions like 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代码为:
The php code for connect.php is:
<?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();
}
?>
推荐答案
您需要为MySQLi
连接指定username
和password
.
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();
}
这篇关于PHP MYSQL无法连接(HY000/2002)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!