用PHP代码如何查询数据库表中的一条记录

要在一个PHP文件中完成,就是在这个文件中有一个表单,里面有一个文本框,一个查询按钮,然后输入表中的某字段内容,点查询,然后就在本页显示查询的内容(与字段相关的一条记录)。 如果要查询查询那条记录中的几个字段内容,那又该怎样写代码呢,谢谢高手帮帮我嘛!!!
就是用PHP代码写一段查询数据库表中的一条记录

比如输入姓名,查询这个人的性别,年龄……而且查询的结果要显示到当前的网页中,也就是说只要一个php文件,里面包含一个表单,表单里面包含一个文本框,一个查询按钮就行了
而且要把表单提交到本页
不晓得怎么了总是查询不到
我提交的代码是:

<?
if(action=='cx')
{
$sql="select * from db where name=$_POST[name]";
$result=mysql_query($sql,$conn);
$row=mysql_fetch_array($result);
echo $row[Name];
echo $row[Age];
echo $row[Classname];
}
?>

我直接在这给你修改答案算了
使用的时候删除行号 修改数据库配置 如果想使用 页面不刷新查询数据库 需要使用JQUERY 如果有需要给我留言
1 <?php
2 if(isset($_POST['submit'])&&$_POST['submit']=='提交'){
3 //判断是否是提交过来的
4 $intext = $_POST['intext'];
5 if($intext!=null||$intext!=''){
6 $link = mysql_connect("localhost", "root", "123456");
7 //数据库配置信息 第一个参数数据库位置第二个是用户名第三个是密码
8 mysql_select_db("szn_test");
9 //设置要使用的数据库
10 $sql = "select * from demo where res = '".$intext."'";
11 //SQL语句
12 var_dump($sql);
13 $res = mysql_query($sql);
14 $arr = array();
15 //吧结果存入数组 并记录数组长度
16 $count = 0;
17 while($data = mysql_fetch_array($res)){
18 $arr[$count] = $data;
19 $count++;
20 }
21 //关闭数据库
22 mysql_close($link);
23 }
24 }
25
26 ?>
27 <html>
28 <head>
29 <title></title>
30 </head>
31 <body>
32 <form id="form1" method="post" action="demo.php">
33 <input type="text" name="intext">
34 <input type="submit" name="submit" value="提交">
35 </form>
36 <?php
37 if(isset($arr)&&$arr != null){
38 for($i = 0; $i < $count; $i++){
39 foreach($arr[$i] as $key => $value){
40 echo "key:".$key." value:".$value;
41 echo " ";
42 }
43 echo "<br>";
44 }
45 }
46 ?>
47 </body>
48 </html>
这个是数据库查询代码 你可以看以下对照着修改修改
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-05
我直接在这给你修改答案算了
使用的时候删除行号
修改数据库配置
如果想使用
页面不刷新查询数据库
需要使用JQUERY
如果有需要给我留言
1
<?php
2
if(isset($_POST['submit'])&&$_POST['submit']=='提交'){
3
//判断是否是提交过来的
4
$intext
=
$_POST['intext'];
5
if($intext!=null||$intext!=''){
6
$link
=
mysql_connect("localhost",
"root",
"123456");
7
//数据库配置信息
第一个参数数据库位置第二个是用户名第三个是密码
8
mysql_select_db("szn_test");
9
//设置要使用的数据库
10
$sql
=
"select
*
from
demo
where
res
=
'".$intext."'";
11
//SQL语句
12
var_dump($sql);
13
$res
=
mysql_query($sql);
14
$arr
=
array();
15
//吧结果存入数组
并记录数组长度
16
$count
=
0;
17
while($data
=
mysql_fetch_array($res)){
18
$arr[$count]
=
$data;
19
$count++;
20
}
21
//关闭数据库
22
mysql_close($link);
23
}
24
}
25
26
?>
27
<html>
28
<head>
29
<title></title>
30
</head>
31
<body>
32
<form
id="form1"
method="post"
action="demo.php">
33
<input
type="text"
name="intext">
34
<input
type="submit"
name="submit"
value="提交">
35
</form>
36
<?php
37
if(isset($arr)&&$arr
!=
null){
38
for($i
=
0;
$i
<
$count;
$i++){
39
foreach($arr[$i]
as
$key
=>
$value){
40
echo
"key:".$key."
value:".$value;
41
echo
"
";
42
}
43
echo
"<br>";
44
}
45
}
46
?>
47
</body>
48
</html>
这个是数据库查询代码
你可以看以下对照着修改修改
第2个回答  2010-07-19
你的意思是说 点击查询后 要吧与关键字相关联的整条记录都显示出来? 那样的话 你要先把这条记录复制 给某个数组,然后输出这个数组就可以了
$sql="select * from db1 where name=$_post[name]";
$result=mysql_query($sql,$con);
$row=mysql_fetch_array($result)
echo $row[name];
echo $row[age];
……
第3个回答  2019-10-09
我直接在这给你修改答案算了
使用的时候删除行号
修改数据库配置
如果想使用
页面不刷新查询数据库
需要使用JQUERY
如果有需要给我留言
1
<?php
2
if(isset($_POST['submit'])&&$_POST['submit']=='提交'){
3
//判断是否是提交过来的
4
$intext
=
$_POST['intext'];
5
if($intext!=null||$intext!=''){
6
$link
=
mysql_connect("localhost",
"root",
"123456");
7
//数据库配置信息
第一个参数数据库位置第二个是用户名第三个是密码
8
mysql_select_db("szn_test");
9
//设置要使用的数据库
10
$sql
=
"select
*
from
demo
where
res
=
'".$intext."'";
11
//SQL语句
12
var_dump($sql);
13
$res
=
mysql_query($sql);
14
$arr
=
array();
15
//吧结果存入数组
并记录数组长度
16
$count
=
0;
17
while($data
=
mysql_fetch_array($res)){
18
$arr[$count]
=
$data;
19
$count++;
20
}
21
//关闭数据库
22
mysql_close($link);
23
}
24
}
25
26
?>
27
<html>
28
<head>
29
<title></title>
30
</head>
31
<body>
32
<form
id="form1"
method="post"
action="demo.php">
33
<input
type="text"
name="intext">
34
<input
type="submit"
name="submit"
value="提交">
35
</form>
36
<?php
37
if(isset($arr)&&$arr
!=
null){
38
for($i
=
0;
$i
<
$count;
$i++){
39
foreach($arr[$i]
as
$key
=>
$value){
40
echo
"key:".$key."
value:".$value;
41
echo
"
";
42
}
43
echo
"<br>";
44
}
45
}
46
?>
47
</body>
48
</html>
这个是数据库查询代码
你可以看以下对照着修改修改
第4个回答  2010-07-20
$sql="select * from db where name=$_POST[name]";
sql语句出错应该写成
$sql="select * from db where name='".$_POST[name]."'";
相似回答