forked from maomaotp/nginx_lua
/
index.php
124 lines (106 loc) · 3.22 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
main();
function main()
{
#解析post请求内容
$keywords = $_POST["keywords"];
$ptype = $_POST["ptype"];
$start = $_POST["start"];
$page = $_POST["page"];
if(!$start){
$start = 0;
}
if(!$page){
$page = 20;
}
$connection = init_db();
$unicode = str2Unicode($keywords);
switch ($ptype)
{
case 1:
$query_sql = "select programId,programName,programUri,programIntro,radioId,albumId,compere,picture,programType,secondLevel,tabSet from a_program where MATCH(searchindex) AGAINST('$unicode') limit $start,$page";
break;
case 2:
$query_sql = "select radioId,nameCn,nameEn,url,webSite,introduction,address,zip,scheduleURL,radioLevel,provinceSpell,cityName,createTime,updateTime,logo,classification,isOffline from Radio_Info where radioState=0 and (nameCn like '%%$keywords%%' or nameEn like '%%$keywords%%') limit $start,$page";
break;
case 3:
$query_sql = "select albumId,albumName,albumIntro,albumType,picture,tabSet from a_album where flag=1 and(albumName like '%%$keywords%%' or tabset like '%%$keywords%%') limit $start,$page";
break;
default:
die('{"describe":"error ptype","code":80012}');
}
$records = array();
$select_res = mysql_query($query_sql) or die("query failed\n");
while($r = mysql_fetch_assoc($select_res)){
$records[] = $r;
}
$json = json_encode($records);
echo $json;
mysql_close($connection);
}
#解析post请求
function init_db()
{
$host = "123.57.41.242";
$db = "fm_appserver";
$user = "lingbanfm";
$pass = "lingban2014";
#连接mysql
$connection = mysql_connect($host, $user, $pass);
if (!$connection){
die("database server connection failed.");
}
mysql_query("set names utf8");
$dbconnect = mysql_select_db($db, $connection);
if (!$dbconnect){
die("unable to connect to the specified database!");
}
return $connection;
}
#编码转换
function str2Unicode($str, $encoding = 'UTF-8')
{
$str = iconv($encoding, 'UCS-2', $str);
$arr = str_split($str, 2);
$unicode = '';
foreach ($arr as $tmp) {
$dec = hexdec(bin2hex($tmp));
$unicode .= $dec . ' ';
}
return $unicode;
}
#增加全文索引字段
function add_index()
{
$connection = init_db();
for($i=0; $i<=2; $i++){
$query = "select programId,programName,compere,tabSet from a_program";
$start = $i*10;
$limit = "limit $start,10";
$select_sql = $query . " " . $limit;
$select_res = mysql_query($select_sql) or die("query failed");
while($r = mysql_fetch_array($select_res)){
$programId = $r["programId"];
$str = $r["programName"] . "" . $r["compere"] . "" . $r["tabSet"];
$unicode = str2Unicode($str);
$update = "update a_program set searchindex='$unicode' where programId='$programId'";
echo "$update\n";
mysql_query($update) or die("update failed");
}
mysql_free_result($select_res);
}
mysql_close($connection);
}
function get_program($keywords,$start,$page)
{
$unicode = str2Unicode($keywords);
$query_sql = "select programId,programName,programUri,programIntro from a_program where MATCH(searchindex) AGAINST('$unicode')order by duration limit $start,$page";
$records = array();
$select_res = mysql_query($query_sql) or die("query failed\n");
while($r = mysql_fetch_assoc($select_res)){
$records[] = $r;
}
$json = json_encode($records);
echo $json;
}
?>