Home >Backend Development >PHP Tutorial >如何把PHP代码转换为thinkPHP的控制器中
<?php header('content-type:text/html;charset=utf-8'); mysql_connect('localhost', 'root' ,''); mysql_select_db("test"); mysql_query("set names utf8"); $queryString = $_POST['queryString']; if(strlen($queryString) >0) { $sql= "SELECT value FROM countries WHERE value LIKE '".$queryString."%' LIMIT 10"; $query = mysql_query($sql); while ($result = mysql_fetch_array($query,MYSQL_BOTH)){ $value=$result['value']; ?> <li onClick="fill('<?=$value?>')"><?=$value?></li> <?php } } ?>
thinkphp中已经有封装好的函数可以用
public function query(){ $where = array(); $limit = 10; if(strlen($queryString)>0){ $where['value'] = array('like','%'.$queryString.'%'); } $list = M('test')->where($where)->limit($limit)->select(); //todo }
public function query(){ $where = array(); $limit = 10; if(strlen($queryString)>0){ $where['value'] = array('like','%'.$queryString.'%'); } $list = M('countries')->field('value')->where($where)->limit($limit)->select(); //todo }