由于要调试JS的AJAX,Java写感觉不够轻便,所以直接捡起了原来的PHP,来做测试用。
最简单的代码
connect.php
- <?php
- $host="localhost";
- $db_user="root";
- $db_pass="";
- $db_name="demo";
- $timezone="Asia/Shanghai";
- $link=mysql_connect($host,$db_user,$db_pass);
- mysql_select_db($db_name,$link);
- mysql_query("SET names UTF8");
- header("Content-Type: text/html; charset=utf-8");
- ?>
search.php
- <?php
- include_once("connect.php");
- $q = strtolower($_GET["term"]);
- $query=mysql_query("select * from test where title like '$q%' limit 0,10");
- while ($row=mysql_fetch_array($query)) {
- $result[] = array(
- 'id' => $row['id'],
- 'label' => $row['title']
- );
- }
- echo json_encode($result);
- ?>
sql
- CREATE TABLE `art` (
- `id` int(11) NOT NULL auto_increment,
- `title` varchar(100) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
- INSERT INTO `art` (`id`, `title`) VALUES
- (1, 'jstest1'),
- (2, 'jstest2'),
- (3, 'jstest3'),
- (4, 'jstest4'),
- (5, 'jstest5'),
- (6, 'jstest6');
分开写的连接数据库
common.ini
- <?php
- //非法调用判断
- if(!defined('Inc_Tag')){
- exit("非法调用");
- }
- //硬路径转换
- define('ROOT_PATH', substr(dirname(__FILE__), 0,-8));
- //版本判断
- if (PHP_VERSION<4.0){
- exit("版本太低");
- }
- require ROOT_PATH.'\includes\global.func.php';
- require ROOT_PATH.'\includes\mysql.func.php';
- define('start_time', _runtime());
- //数据库连接
- define('DB_HOST','Localhost');
- define('DB_NAME','test');
- define('DB_USER','root');
- define('DB_PASSWORD','123456');
- //初始化数据库
- _connect(); //连接MYSQL数据库
- _select_db(); //选择指定的数据库
- _set_names(); //设置字符集
- ?>
mysql.php
- <?php
- //防止恶意调用
- if(!defined('Inc_Tag')){
- exit();
- }
- /**
- _connect()数据库连接函数
- */
- function _connect() {
- //global表示全局变量的意思,意思是此变量在函数外部也可使用
- global $_conn;
- if(!$conn=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)){
- exit("数据库连接失败".mysql_error());
- }
- }
- /**
- *
- * _select_db()选择数据库
- */
- function _select_db(){
- if(!mysql_select_db(DB_NAME)){
- exit("ERROR".mysql_error());
- }
- }
- /**
- *
- * _set_names()设置字符集
- */
- function _set_names(){
- if(!mysql_query('SET NAMES UTF8')){
- exit('ERROR'.mysql_error());
- }
- }
- /**
- *
- * _query()查询语句
- * @param unknown_type $sql
- */
- function _query($_sql){
- if (!$_result=mysql_query($_sql)){
- exit('SQL FAILED'.mysql_error());
- }
- return $_result;
- }
- /**
- *
- * Enter description here ...
- * @param unknown_type $_sql
- */
- function _fetch_array($_sql){
- return mysql_fetch_array(_query($_sql),MYSQL_ASSOC);
- }
- /**
- *
- * fetch_array_list($_result)在结果中读取关联数组
- * @param unknown_type $_result
- */
- function _fetch_array_list($_result){
- return mysql_fetch_array($_result,MYSQL_ASSOC);
- }
- /**
- *
- * Enter description here ...
- * @param unknown_type $_sql
- * @param unknown_type $_info
- */
- function _is_repeat($_sql,$_info){
- if(_fetch_array($_sql)){
- _alert_back($_info);
- }
- }
- /**
- *
- * Enter description here ...
- */
- function _affected_rows() {
- return mysql_affected_rows();
- }
- /**
- *
- * _close关闭数据库连接
- */
- function _close(){
- if (!mysql_close()){
- exit("关闭异常".mysql_error());
- }
- }
- ?>
global.fun
- <?php
- function _runtime(){
- $_mtime=explode(' ', microtime());
- $_temp_time=$_mtime[1]+$_mtime[0];
- return $_temp_time;
- }
- /**
- *
- * _alert_back()对话框弹出函数,然后返回一步
- * @param $msg //弹出对话框消息
- * @return void
- */
- function _alert_back($msg){
- echo "<script type='text/javascript'>alert('".$msg."');history.back();</script>";
- exit();
- }
- /**
- *
- * _alert_close()弹出消息,关闭
- * @param unknown_type $msg
- */
- function _alert_close($msg){
- echo "<script type='text/javascript'>alert('".$msg."');window.close();</script>";
- exit();
- }
- function _get_xml($_xml_file){
- if (file_exists($_xml_file)){
- $_xml=file_get_contents($_xml_file);
- preg_match_all('/<vip>(.*)<\/vip>/s',$_xml,$_dom);
- echo $_dom[1];
- foreach ($_dom[1] as $_value) {
- preg_match_all('/<id>(.*)<\/id>/s',$_value,$_id);
- print_r($_id);
- preg_match_all('/<username>(.*)<\/username>/s',$_value,$_username);
- preg_match_all( '/<sex>(.*)<\/sex>/s', $_value, $_sex);
- preg_match_all( '/<face>(.*)<\/face>/s', $_value, $_face);
- preg_match_all( '/<email>(.*)<\/email>/s', $_value, $_email);
- preg_match_all( '/<url>(.*)<\/url>/s', $_value, $_url);
- $_html['id'] = $_id[1][0];
- $_html['username'] = $_username[1][0];
- $_html['sex'] = $_sex[1][0];
- $_html['face'] = $_face[1][0];
- $_html['email'] = $_email[1][0];
- $_html['url'] = $_url[1][0];
- }
- } else {
- echo '文件不存在';
- }
- return $_html;
- }
- /**
- *
- * Enter description here ...
- * @param unknown_type $_xmlfile
- * @param unknown_type $_clean
- */
- function _set_xml($_xmlfile,$_clean) {
- $_fp = @fopen('new.xml','w');
- if (!$_fp) {
- exit('系统错误,文件不存在!');
- }
- flock($_fp,LOCK_EX);
- $_string = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "<vip>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<id>{$_clean['id']}</id>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<username>{$_clean['username']}</username>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<sex>{$_clean['sex']}</sex>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<face>{$_clean['face']}</face>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<email>{$_clean['email']}</email>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "\t<url>{$_clean['url']}</url>\r\n";
- fwrite($_fp,$_string,strlen($_string));
- $_string = "</vip>";
- fwrite($_fp,$_string,strlen($_string));
- flock($_fp,LOCK_UN);
- fclose($_fp);
- }
- /**
- *
- * _location()跳转函数
- * @param unknown_type $_msg
- * @param unknown_type $_url
- */
- function _location($_msg,$_url){
- if (!empty($_msg)){
- echo "<script type='text/javascript'>alert('$_msg');location.href='$_url';</script>";
- exit();
- }else {
- header('location:'.$_url);
- }
- }
- function _title($_string){
- if(mb_strlen($_string,'utf-8')>14){
- return mb_substr($_string, 1,6,'utf-8').'...';
- }
- return $_string;
- }
- /**
- *
- * _html()对数组或者字符串进行html过滤
- * @param array or string $_string
- * @return string or array
- */
- function _html($_string){
- if (is_array($_string)){
- foreach ($_string as $_key=>$_value){
- $_string[$_key]=_html($_value);//采用递归对其进行html过滤
- }
- }else {
- $_string=htmlspecialchars($_string);
- }
- return $_string;
- }
- /**
- *
- * _page()对查询的进行分页处理,
- * @param unknown_type $_sql
- * @param unknown_type $_size
- */
- function _page($_sql,$_size) {
- //将里面的所有变量取出来,外部可以访问
- global $_page,$_pagesize,$_pagenum,$_pageabsolute,$_num;
- if (isset($_GET['page'])) {
- $_page=$_GET['page'];
- if (empty($_page) || $_page < 0 || !is_numeric($_page)) {
- $_page=1;
- } else {
- $_page=intval($_page);
- }
- } else {
- $_page=1;
- }
- $_pagesize=$_size;
- $_num=mysql_num_rows(_query($_sql));
- if ($_num==0) {
- $_pageabsolute=1;
- } else {
- $_pageabsolute=ceil($_num/$_pagesize);
- }
- if ($_page>$_pageabsolute) {
- $_page=$_pageabsolute;
- }
- $_pagenum=($_page - 1) * $_pagesize;
- }
- /**
- *
- */
- function _paging($_type) {
- global $_page,$_pageabsolute,$_num;
- if ($_type==1) {
- echo '<div id="page_num">';
- echo '<ul>';
- for ($i=0;$i<$_pageabsolute;$i++) {
- if ($_page==($i+1)) {
- echo '<li><a href="'.Script.'.php?page='.($i+1).'" class="selected">'.($i+1).'</a></li>';
- } else {
- echo '<li><a href="'.Script.'.php?page='.($i+1).'">'.($i+1).'</a></li>';
- }
- }
- echo '</ul>';
- echo '</div>';
- } elseif ($_type==2) {
- echo '<div id="page_text">';
- echo '<ul>';
- echo '<li>'.$_page.'/'.$_pageabsolute.'页 | </li>';
- echo '<li>共有<strong>'.$_num.'</strong>个会员 | </li>';
- if ($_page==1) {
- echo '<li>首页 | </li>';
- echo '<li>上一页 | </li>';
- } else {
- echo '<li><a href="'.SCRIPT.'.php">首页</a> | </li>';
- echo '<li><a href="'.SCRIPT.'.php?page='.($_page-1).'">上一页</a> | </li>';
- }
- if ($_page==$_pageabsolute) {
- echo '<li>下一页 | </li>';
- echo '<li>尾页</li>';
- } else {
- echo '<li><a href="'.SCRIPT.'.php?page='.($_page+1).'">下一页</a> | </li>';
- echo '<li><a href="'.SCRIPT.'.php?page='.$_pageabsolute.'">尾页</a></li>';
- }
- echo '</ul>';
- echo '</div>';
- }
- }
- function _unsetcookies(){
- setcookie('username','',time()-1);
- setcookie('uniqid','',time()-1);
- session_destroy();
- _location(null,'index.php');
- }
- /**
- * _code() 注册码函数
- * @access public
- * @param $_width 验证码图片宽
- * @param $_height 验证码图片高
- * @param $_rnd_code 验证码位数
- * @param $_flag 是否显示黑色边框
- * @return void 返回一验证码图片
- */
- function _code($_width=75,$_height=25,$_rnd_code=4,$flag=FALSE){
- //创建随机码
- for ($i=0;$i<$_rnd_code;$i++) {
- $_nmsg .= dechex(mt_rand(0,15));
- }
- //保存在session
- $_SESSION['code'] = $_nmsg;
- //创建一张图像
- $_img = imagecreatetruecolor($_width,$_height);
- //白色
- $_white = imagecolorallocate($_img,255,255,255);
- //填充
- imagefill($_img,0,0,$_white);
- $_flag = false;
- if ($_flag) {
- //黑色,边框
- $_black = imagecolorallocate($_img,0,0,0);
- imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
- }
- //随即画出6个线条
- for ($i=0;$i<6;$i++) {
- $_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
- }
- //随即雪花
- for ($i=0;$i<100;$i++) {
- $_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
- }
- //输出验证码
- for ($i=0;$i<strlen($_SESSION['code']);$i++) {
- $_rnd_color = imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
- imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10),mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color);
- }
- //输出图像
- header('Content-Type: image/png');
- imagepng($_img);
- //销毁
- imagedestroy($_img);
- }
- function _sha1_uniqid(){
- return sha1(uniqid(rand(),TRUE));
- }
- ?>
查询
- <?php
- /**
- * TestGuest Version1.0
- * ================================================
- * Copy 2010-2012 yc60
- * Web: http://www.yc60.com
- * ================================================
- * Author: Lee
- * Date:2011-9-7
- */
- error_reporting(E_ALL^E_NOTICE);
- //定义常量,授权includes的包含文件
- define('Inc_Tag', TRUE);
- //定义常量,包含CSS文件
- define('Script', 'blog');
- //包含公共文件
- require dirname(__FILE__).'/includes/common.inc.php';
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>hello gull记事本-博友界面</title>
- <?php
- //包含CSS文件
- require ROOT_PATH.'includes/title.inc.php';
- ?>
- </head>
- <body>
- <?php
- //包含头文件
- require ROOT_PATH.'includes/header.inc.php';
- //分页模块
- global $_pagesize,$_pagenum;
- _page("SELECT dg_id FROM tg_user",10); //第一个参数获取总条数,第二个参数,指定每页多少条
- //读取数据库信息
- $_result=_query("SELECT t_username,t_sex,t_face FROM tg_user ORDER BY t_reg_time DESC limit $_pagenum,$_pagesize");
- ?>
- <div id="blog">
- <div>
- <h2>博友信息</h2>
- <?php while (!!$rows=_fetch_array_list($_result)){?>
- <dl>
- <dd class="user"><?php echo $rows['t_username']?>(<?php echo $rows['t_sex']?>)</dd>
- <dt><img src="<?php echo $rows['t_face']?>" /></dt>
- <dd class="message">发消息</dd>
- <dd class="friend">加为好友</dd>
- <dd class="guest">写留言</dd>
- <dd class="flower">给他送花</dd>
- </dl>
- <?php }?>
- <?php
- _paging(1);
- ?>
- </div>
- <?php
- //包含尾部文件
- require ROOT_PATH.'includes/footer.inc.php';
- ?>
- </body>
- </html>