php查询的问题
我做了个php查询界面,
<html>
<head><title>查询结果</title></head>
<body>
<?
include("connMysql.php");
$quer=$_REQUEST["quer"];
?>
<table table border="1" width="20%" align="center">
<?
if ($quer["Name"]!='')
{
$tel_quer="SELECT *
FROM students
WHERE Name LIKE '%".$quer."%'";
echo $tel_quer;
$quer = mysql_query($tel_quer);
while($tel_quer=mysql_fetch_array($quer)){
echo "<tr>";
echo "<td rowspan=3 align=center><img src=".$tel_quer["photo"]."></td>";
echo "<td align=center>姓名:".$tel_quer["Name"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >性别:".$tel_quer["Sex"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >电话:".$tel_quer["phone"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2 align=center >地址:".$tel_quer["Arrd"]."</td>";
echo "</tr>";
}
}
else{
echo "<p align=center>抱歉! 资料库没有此项资料!</p>";
}
?>
</table>
<p align="center"><a href="tel.php">返回主页面</a></p>
</body>
</html>
如果数据库没有这项资料 就显示 “抱歉! 资料库没有此项资料!”这样的if句应该怎么写?
答案:<html>
<head><title>查询结果</title></head>
<body>
<?php
include("connMysql.php");
$quer=$_REQUEST["quer"];
echo "<table table border=\"1\" width=\"20%\" align=\"center\">";
if ($quer["Name"]!='')
{
//查询数据库学生信息有多少条
$sql="SELECT * FROM students WHERE Name LIKE '%".$quer."%'";
$result = mysql_query($sql);
$nums=mysql_num_rows($result);
//或者亦可以用count
//$sql="SELECT count(-1) as num * FROM students WHERE Name LIKE '%".$quer."%'";
if($nums>0)
{
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td rowspan=3 align=center><img src=".$row["photo"]."></td>";
echo "<td align=center>姓名:".$row["Name"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >性别:".$row["Sex"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >电话:".$row["phone"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2 align=center >地址:".$row["Arrd"]."</td>";
echo "</tr>";
}
}
else
{
echo "<p align=center>抱歉! 资料库没有此项资料!</p>";
}
}
?>
</table>
<p align="center"><a href="tel.php">返回主页面</a></p>
</body>
</html>
其他:给你修改了一下,注意变量的命名不要乱:
<html>
<head><title>查询结果</title></head>
<body>
<?php
include("connMysql.php");
$quer=$_REQUEST["quer"];
echo "<table table border=\"1\" width=\"20%\" align=\"center\">";
if ($quer["Name"]!='')
{
$sql="SELECT *
FROM students
WHERE Name LIKE '%".$quer."%'";
$result = mysql_query($sql);
$nums=mysql_num_rows($result);
if($nums>0)
{
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td rowspan=3 align=center><img src=".$row["photo"]."></td>";
echo "<td align=center>姓名:".$row["Name"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >性别:".$row["Sex"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center >电话:".$row["phone"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=2 align=center >地址:".$row["Arrd"]."</td>";
echo "</tr>";
}
}
else
{
echo "<p align=center>抱歉! 资料库没有此项资料!</p>";
}
}
?>
</table>
<p align="center"><a href="tel.php">返回主页面</a></p>
</body>
</html> if (exists($quer["Name"])){
echo "抱歉";
}
上一个:网页制作相关的三种语言:ASP JSP PHP那个好点,简单点?
下一个:发布数据时检查数据库是否已经存在 [ php、mysql ]