Home  >  Article  >  php教程  >  php 数据库内容搜索,搜索指定内容并输出

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

WBOY
WBOYOriginal
2016-05-24 18:31:031804browse

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

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

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

保存数据,代码如下:

insert into `acc`.`search` ( 
`id` , 
`keyword`  
) 
values ( 
null , '内容搜索' 
), ( 
null , 'php站内搜索' 
);

数据连接,代码如下:

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


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