header('Access-Control-Allow-Origin: *'); //解决跨域问题
header('Content-type: text/json'); //定义编码JSON
//定义函数,输出json
function json($result_number,$result=array()){
$result=array(
'result_number'=>$result_number,
'result'=>$result
);
//输出json
echo json_encode($result);
exit;
}
//解决 PHP 中提示的undefined index
function _post($str){
$val = !empty($_POST[$str]) ? $_POST[$str] : null;
return $val;
}
$content=_post('content');
$conn = mysqli_connect("localhost","root",""," myplays");
mysqli_query($conn,"set names utf8");
$sql = "select videourl,videotitle from video where videotitle='$content'";
$resultSet = mysqli_query($conn,$sql);
$sqli = mysqli_num_rows($resultSet);
$dataarr = array();
if(mysqli_num_rows($resultSet)>0){
// echo $sqli;
while($row = mysqli_fetch_assoc($resultSet)) {
// echo $row["videourl"];
// echo $row["videotitle"];
$dataarr[]=$row;
}
echo json(1,$dataarr);
}else{
echo '0';
}
?>
PHP写APP后端接口,读取数据库中的数据并返回json数据
最新推荐文章于 2024-07-09 07:06:31 发布