从数据库获取数据转为json格式给程序调用

<?php
header("content-Type: text/html; charset=utf-8");//字符编码设置
$servername = "localhost";
$username = "wang_shuchuwenku";
$password = "wang123456";
$dbname = "wang_shuchuwenku";

$tosearch = $_GET['tosearch'];

// 创建连接
$conn =new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "

SELECT 
`vod_name` as '名称'
,`vod_class` as '类型'
,`vod_year` as '年份'
,`vod_play_url` as '播放地址'
,`vod_area` as '地区'
,`vod_director` as '导演'
,`vod_actor` as '演员'
,`vod_content` as '内容'  
FROM `mac_vod` 
WHERE  `vod_name` Like '%$tosearch%'  
OR `vod_class` Like '%$tosearch%'  
OR `vod_class` Like '%$tosearch%' 

";
$result = $conn->query($sql);
 //SELECT * FROM `mac_vod`
$arr = array();
// 输出每行数据
while($row = $result-> fetch_assoc()) {
    $count=count($row);//不能在循环语句中,由于每次删除row数组长度都减小
    for($i=0;$i<$count;$i++){
        unset($row[$i]);//删除冗余数据
    }
    array_push($arr,$row);
 
}
//print_r($arr);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);//json编码
$conn->close();
 
?>

假设上面为文件test2.php

放置站点根目录

 

注备:少量数据可用

调用方法:http://  ip或者域名  /test2.php/tosearch?= 搜索内容

上一篇:阿里云VOD(二)


下一篇:代码二次开发edusoho网校系统中CDN阿里云视频加速播放的解决方案