怎么把MySQL数据库表的内容,通过数据表中的数据检索为条件,以表单的形式,显示在PHP页面。

数据库名为db_online,数据表名为tb_user,,数据表字段为online_id,online_user,online_number,online_pass,检索条件为以字段online_number,online_pass,PHP页面为chaxun.php。本人是新手,不要说太专业。求详细的代码和注释。因为急需,本人在线等。

编写调试代码,回答有点晚了,以下是一个比较完整的示例:

下面输出一个表格,两个输入框,输入用户号和密码:

<form id="form1" name="form1" method="post" action="chaxun.php">
<table width="344" height="136" border="0" align="center" cellpadding="2" cellspacing="2">
<td width="100" height="35" align="right">用户号:</td>
<td width="275"><input type="text" name="online_number" id="online_number" width="150" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td height="35"><input type="password" name="online_pass" id="online_pass" width="150" /></td>
</tr>
<tr>
<td height="45" colspan="2" align="center"><input type="submit" value="注册" /> </td>
</tr>
</table>
</form>

<?php
//下面编写处理代码,有说明

header('Content-type:text/html;charset=utf-8');//设置编码格式为utf8
if(isset($_POST['online_number']) && isset($_POST['online_pass']))
{
$db = new mysqli('localhost','test','test','db_online');//建立数据库连接,用户名、密码、数据库根据实际修改
$pass = $_POST['online_pass'];
$num = $_POST['online_number'];

$rows = $db->query("SELECT * FROM tb_user WHERE online_number = $num AND online_pass ='$pass'");
$r_num = $rows->num_rows;
if($r_num>0)
{
echo '<table border="1"><tr>';
$row = $rows->fetch_row();
for($i=0;$i<count($row);$i++)
{
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
} else
echo '<script>alert("用户号或密码错误");</script>';
}
?>

将以上代码保存为chaxun.php,修改你实际的数据库连接 用户名和密码,运行追问

if(mysql_num_rows($sql)){
while($row = mysql_fetch_array($sql)){
echo $row["online_id"."online_user"."online_number"."online_pass];
}
}
else{
echo "暂无记录";
}
mysql_free_result($sql);
?>

不知道这样行不行呢?

追答

不需要用while结构吧?因为从逻辑上看,online_number + online_pass 得到的结果应该是唯一的才对。

所以,用
  $r_num = $rows->num_rows;
if($r_num>0)

这样判断结果是不是大于零就可以了。

我编写的代码是用对象,你用的普通的过程函数也可以,结果是一样的。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-07
你会连接数据库吗?追问

数据库会连,就是对具体显示数据表的内容不知道怎么下手。对PHP语言还不是很懂。

追答

会连数据库就简单了,
写条sql语句再循环输出就可以了,给你来个简单的例子。
$sql = mysql_query("select * from 表名 where 条件 order by 排序字段 asc或desc",$conn);
if(mysql_num_rows($sql)){
while($row = mysql_fetch_array($sql)){
echo $row[字段名];//多个字段就用“.”连接起来输出。自己组合着用。
}
}else{
echo "暂无记录";
}
mysql_free_result($sql);

追问

<?php
include("conn/query.php");
$online_number=$_POST['online_number'];
$online_pass=$_POST['online_pass'];
$sql = mysql_query("select * from tb_user where online_number='$online_number' and online_pass='$online_pass' order by online_id,online_user,online_number,online_pass from tb_user ORDER BY online_id",$link);

追答

include("conn/query.php");
$online_number=$_POST['online_number'];
$online_pass=$_POST['online_pass'];
$sql = mysql_query("select * from tb_user where online_number='$online_number' and online_pass='$online_pass' order by online_id,online_user,online_number,online_pass",$link);

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网