简易PHP连接数据库并输出JSON

2023-08-24 10:45:09 浏览数 (1)

直接源码

代码语言:javascript复制
<?php
$dbhost = 'localhost'; // MySQL服务器主机地址
$dbuser = 'wx_wfk168_com'; // mysql用户名
$dbname = 'wx_wfk168_com'; //数据库名称
$dbpass = 'xDNRHJR5A3X8r8E8'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(!$conn){
    die('连接失败: ' . mysqli_error($conn));
}
$json = '';
$data = array(); 
$sql = "SELECT * FROM ims_longbing_card_timeline where id = ".$_GET['id'];//sql查询语句
$result = mysqli_query($conn,$sql);
if($result){
    while($row = mysqli_fetch_assoc($result)){
        $user = new Class{}; //定义空对象
        $user->id = $row["id"];
        $user->title = urlencode($row["title"]);
        $user->content = $row["content"];
        $data[]=$user;
      }
      $json = json_encode($data);//把数据转换为JSON数据.
      echo "{".'"data"'.":".urldecode($json)."}";
 }else{
    echo "查询失败";
 }
?>

0 人点赞