首页 > 开发 > PHP > 正文

php 数据库内容搜索,搜索指定内容并输出

2024-05-04 21:59:08
字体:
来源:转载
供稿:网友

实现原理很简单就是根据用户提交的数据到mysql数据表中查询是否有相同的,有就输出没有就不操作了,这是一款简单的记录搜索代码.

首先我们来创建搜索数据表:

  1. create table if not exists `search` ( 
  2.   `id` int(4) not null auto_increment, 
  3.   `keyword` varchar(500) not null
  4.   primary key  (`id`) 
  5. ) engine=myisam default charset=utf8 auto_increment=1 ; 

保存数据,代码如下:

  1. insert into `acc`.`search` ( 
  2. `id` , 
  3. `keyword`  
  4. values ( 
  5. null , '内容搜索' 
  6. ), ( 
  7. null , 'php站内搜索' 
  8. ); 

数据连接,代码如下:

  1. $conn = mysql_connect('localhost','ac','1');//这里的ac用户名,1是密码,修改成你自己的mysql便可 
  2. $res = mysql_db_query('abc',"select * from search where keyword='内容搜索' "or die(mysql_error()); 
  3. if(mysql_num_rows($res) == 1) { 
  4.    while($row = mysql_fetch_assoc($res)) { 
  5.       foreach($row as $key => $val) { 
  6.          echo $row['keyword'],'<br >'
  7.       }//开源代码Vevb.com 
  8.    } 
  9. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表