无法使用PHP连接MYSQL;拒绝用户访问''@';本地主机';(使用密码:否)";

无法使用PHP连接MYSQL;拒绝用户访问''@';本地主机';(使用密码:否)";,php,mysql,Php,Mysql,我正在尝试连接php和mysql 以下是代码: <?php $response=array(); require_once 'C:\wamp\www\android_connect\db_connect.php'; $db=new DB_CONNECT(); $result=mysql_query("select * from product")or die(mysql_error()); if(mysql_num_rows($result)>0) { $respon

我正在尝试连接php和mysql

以下是代码:

<?php

$response=array();
require_once 'C:\wamp\www\android_connect\db_connect.php';
$db=new DB_CONNECT();


$result=mysql_query("select * from product")or die(mysql_error());

if(mysql_num_rows($result)>0)
{
    $response["products"]=array();

    while($row=mysql_fetch_array($result))
    {
        $product=array();
        $product["pid"]=$row["pid"];
        $product["name"]=$row["name"];
        $product["price"]=$row["price"];
        $product["description"]=$row["description"];

        array_push($response["products"],$product);
    }
    $response["success"]=1;
}
else
{
    $response["success"]=0;
    $response["message"]="No products found";
}
echo json_encode($response);

?>
Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\wamp\www\android_connect\get_all_products.php on line 8

Warning: mysql_query(): A link to the server could not be established in C:\wamp\www\android_connect\get_all_products.php on line 8
就我而言,第8行是:

$result=mysql_query("select * from product")or die(mysql_error());
db_connect
代码如下所示:

<?php

class DB_CONNECT
{

    function _construct()
    {
        $this->connect();
    }

    function _destruct()
    {
        $this->close();
    }

    function connect()
    {
        $con=mysql_connect('localhost','root','kamani') or die (mysql_error());
        $db=mysql_select_db('mobileinventory') or die (mysql_error());
        return $con;
    }

    function close()
    {
        mysql_close();
    }
}

?>

为了完成查看错误,我上传了它的快照。


无法解决此错误

您必须为神奇方法添加两个下划线:

function __construct() {
  $this->connect();
}
在其他地方,构造函数和connect方法将永远不会被调用。与_destruct()方法相同

您不应该使用PHP的mysql扩展,因为它已被弃用,并在PHP的下一个版本中被删除。请改用PDO或mysqli。

您必须这样做

$db=new DB_CONNECT();
$db->connect();

似乎您没有连接到数据库,或者在数据库文件中修复@TiMESPLiNTER建议,您必须将构造函数(和析构函数)也更改为

 function __construct()
 {
    $this->connect();
}

function __destruct()
{
    $this->close();
}

通过删除密码进行检查。输入空密码。侧注:停止使用不推荐使用的
mysql\uuquot
函数。使用或替代。连接在数据库文件的构造函数中被调用承包商没有被调用,因为它缺少下划线。\u你能告诉我更多关于PDO和mysqli的信息吗?我的主要目的是开发android应用程序。与android无关,它们是连接MySQL数据库服务器的PHP库。请参见此处:和此处