php访问数据库然后json解析,来做一个接口,请大神帮忙看看我写的有哪些问题并给我指出来好嘛,
<?php$host=’1027.0.0.1’;$root=‘root’;$pwd=‘XXXXXXXXX’;$con=mysql_connect($host,$root,...
<?php
$host = ’1027.0.0.1’;
$root = ‘root’;
$pwd = ‘XXXXXXXXX’;
$con = mysql_connect($host,$root,$pwd);
if($con == false){
echo”连接数据库失败”;
}else{
echo “连接数据库成功”;
}
?>
<?php
include’conn.php’;
$sql=“select *from user”;
$result=mysql_query$sql);
$json=“”;
$data =array();
class User
{
public $name;
public $id;
}
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$user = new User();
$user->name=$row[“name”];
$user->id=$row[“id”];
$data[]=$user;
}
$json=json_encode($data);
echo”{“.’”user”’.”:”.$json’}”;
?> 展开
$host = ’1027.0.0.1’;
$root = ‘root’;
$pwd = ‘XXXXXXXXX’;
$con = mysql_connect($host,$root,$pwd);
if($con == false){
echo”连接数据库失败”;
}else{
echo “连接数据库成功”;
}
?>
<?php
include’conn.php’;
$sql=“select *from user”;
$result=mysql_query$sql);
$json=“”;
$data =array();
class User
{
public $name;
public $id;
}
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$user = new User();
$user->name=$row[“name”];
$user->id=$row[“id”];
$data[]=$user;
}
$json=json_encode($data);
echo”{“.’”user”’.”:”.$json’}”;
?> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询