您的位置:首页 > 编程语言 > PHP开发

PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能

2018-05-31 10:18 1061 查看

本文实例讲述了PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能。分享给大家供大家参考,具体如下:

一、代码

td{
font-size:9pt;
}
.style2 {color: #FFFFFF}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="style.css" rel="external nofollow" rel="stylesheet">
<title>应用mysql_num_rows()函数获取查询结果集中的记录数</title>
</head>
<body>
<table width="609" height="134" border="1" cellpadding="0" cellspacing="0" bgcolor="#9E7DB4" align="center">
<form name="myform" method="post" action="">
<tr>
<td width="605" height="51" bgcolor="#CC99FF"><div align="center">请输入图书名称
<input name="txt_book" type="text" id="txt_book" size="25" >
 
<input type="submit" name="Submit" value="查询">
</div></td>
</tr>
</form>
<tr valign="top" bgcolor="#FFFFFF">
<td height="81">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="79" align="right" valign="top"> <br>
<table width="572" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#625D59">
<tr align="center" bgcolor="#CC99FF">
<td width="46" height="20">编号</td>
<td width="167">图书名称</td>
<td width="90">出版时间</td>
<td width="70">图书定价</td>
<td width="78">作者</td>
<td width="114">出版社</td>
</tr>
<?php
$link=mysql_connect("localhost","root","root") or die("数据库连接失败".mysql_error());
mysql_select_db("db_database13",$link);
mysql_query("set names gb2312");
$sql=mysql_query("select * from tb_book");
$info=mysql_fetch_object($sql);
if ($_POST[Submit]=="查询"){
$txt_book=$_POST[txt_book];
$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'"); //如果选择的条件为"like",则进行模糊查询
$info=mysql_fetch_object($sql);
}
if($info==false){ //如果检索的信息不存在,则输出相应的提示信息
echo "<div align='center' style='color:#FF0000; font-size:12px'>对不起,您检索的图书信息不存在!</div>";
}
do{
?>
<tr align="left" bgcolor="#FFFFFF">
<td height="20" align="center"><?php echo $info->id; ?></td>
<td > <?php echo $info->bookname; ?></td>
<td align="center"><?php echo $info->issuDate; ?></td>
<td align="center"><?php echo $info->price; ?></td>
<td align="center"> <?php echo $info->maker; ?></td>
<td> <?php echo $info->publisher; ?></td>
</tr>
<?php
}while($info=mysql_fetch_object($sql));
?>
</table>
<br>
       找到相关记录 <?php $nums=mysql_num_rows($sql);echo $nums;?> 条    </td>
</tr>
</table>
<br></td>
</tr>
</table>
</body>
</html>

二、运行结果

注意:这里使用

mysql_num_rows()
返回结果集中行的数目。此命令仅对 SELECT 语句有效。要取得被 INSERTUPDATE 或者 DELETE 查询所影响到的行的数目,需要使用
mysql_affected_rows()

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP MySQL 模糊查询