Home >Backend Development >PHP Tutorial >如何把PHP代码转换为thinkPHP的控制器中

如何把PHP代码转换为thinkPHP的控制器中

WBOY
WBOYOriginal
2016-06-23 13:03:231248browse

<?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           }        }         ?>  

这是PHP代码,如何将这个代码转换为thinkPHP的Aciton中,求大神帮忙。


回复讨论(解决方案)

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    }


上面写错啦

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn