阅读背景:

PHP写APP后端接口,读取数据库中的数据并返回json数据_小虎工程师的博客_php 读数据库返回json

来源:互联网 
 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';
} 

?>
 header('Access-Control-Allow-Origin: *'



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: