您的位置:首页 > 编程语言 > PHP开发

PHP + Mysql 学习之三(获取数据)

2017-04-15 00:00 351 查看
<?php
header("Content-Type: text/html; charset=utf-8") ;
$distributeId = $_GET['distributeId'];

$user = new mysqli();
if($user)
{
$user->connect("localhost", "root", "123456", "test_db");
if (mysqli_connect_errno())
{
$arr = array(
'code'=>'400',
'info'=>"系统错误!"
);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
exit();
}
}
else
{
$arr = array(
'code'=>'400',
'info'=>"系统错误!",
);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
exit();
}

$user->query("set names 'utf8' ");

$query = "SELECT * FROM testtbl where distributeId = '$distributeId'";
//Use a variable to save result
if($result = $user->query($query))
{
$arr = array(
'code'=>'200',
'info'=>"retrieve data successfully!",
'num'=>$result->num_rows,
'data'=>$result->fetch_all()
);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);

}
else
{
$arr = array(
'code'=>'400',
'info'=>"获取数据失败!"
);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);

}

$user->close();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP Mysql