我正在尝试连接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);
?>
当我尝试使用WAMP
安装在我的计算机上打开文件时,它会引发以下错误:
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();
}
}
?>
要在此处完成查看错误,我正在上传其快照。
我无法解决这个错误。