php查询数据表输出json问题
本帖最后由 Zihao88 于 2020-7-12 16:46 编辑是这样,我在做API时,需要实现查询数据表内某个相同字段的数量,但是查询出来输出的结果带有{count(*):1}的数据表整体格式,怎样才能只输出count:1(count指第一个)呢?
下面是PHP查询代码
希望有大佬可以帮忙解决一下,万分感谢! <?php
//设置json格式头部,并防止出现跨域问题
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');
require_once ('../comm/conn.php');
mysqli_select_db($conn,"wish");
$sql="select * from wisher";
$result=mysqli_query($conn,$sql) or die('查询数据失败:'.mysqli_errno($conn));
$json = '';
$data = array();
class Note
{
public $id;
public $content;
public $sender;
public $likeCount;
public $time;
}
if($result){
while ($row = mysqli_fetch_array($result,MYSQLI_BOTH))
{
$note = new Note();
$note->id = $row["id"];
$note->content = $row["content"];
$note->sender = $row["sender"];
$note->likeCount = $row["likeCount"];
$note->time = $row["time"];
$data[]=$note;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo "{".'"code"'.":1,".'"note"'.":".$json."}";
mysqli_close($conn);
}else{
echo "{".'"code"'.":0,"."}";
}
?>
$count = select count(id) as id from .....
$data = [
'count'=>$count['id']
];
return json($data); sql 语句有问题,改下
select count(tid) as c from xxx_article where tid=$id
取值的时候,可以取 $count['c'] 千百度 发表于 2020-7-12 15:50
sql 语句有问题,改下
select count(tid) as c from xxx_article where tid=$id
[ ...
可以了,感谢{:1_893:}
页:
[1]