php的查询功能(如何通过查询条件获取相应表的字段)

2016 年 7 月 13 日2810

用户提出问题:php的查询功能(如何通过查询条件获取相应表的字段),具体如下:

通过互联网整理获得以下解决方法:

=================1楼=====================

请描述下你遇到的问题

=================2楼=====================

result=mysql_query("select*frombookswherechecked=‘$checked’");

=================3楼=====================

比如输入要查询的isbn号,然后确定,在下方输出符合条件的查询结果。

=================4楼=====================

result=mysql_query("select*frombookswherechecked=‘$checked’");这个我试了一下,出现以下错误

还有一个问题要请教是如何是要先查询之后才显示表要显示的结果。

=================5楼=====================

...where字段='$checked'//字段换成你实际的字段

=================6楼=====================

if(isset($_POST['checked'])){

@$result=mysql_query("select*frombooks",$db);

echo"<tableborder=1>\n";

echo"<tr>

<td>isbn</td>

<td>author</td>

<td>title</td>

<td>price</td>

";

echo"</tr>\n";

//循环遍历

while($myrow=mysql_fetch_row($result)){

printf("<tr>

<td>%s</td>

<td>%s</td>

<td>%s</td>

<td>%s</td>",$myrow[0],$myrow[1],$myrow[2],$myrow[3]);

}

echo"</table>\n";

}

=================7楼=====================

谢谢jordan102,那请问如何通过查询条件,然后在同个页面下输出符合查询条件的表呢?

这是我的表

=================8楼=====================

我试了jordan102的代码,也不知道我添加if(isset($_POST['checked'])){.......}这段代码的位置对不对。

我按了下查询的按钮,但是没反应。代码如下

<?php




//获得连接




$db=mysql_connect("localhost","root","root")ordie(mysql_error());




//echo"ConnectedtoMySQL<br/>";




//连接数据库




mysql_select_db("test")ordie(mysql_error());




//echo"ConnectedtoDatabase";









//查询数据,并用表格显示出来




@$checked=$_POST['checked'];




// @$result=mysql_query("select*frombooks",$db);




// @$result=mysql_query("select*frombookswhereisbn='$checked'");




if(isset($_POST['checked'])){




@$result=mysql_query("select*frombooks",$db);




echo"<tableborder=1>\n";




echo"<tr>




<td>isbn</td>




<td>author</td>




<td>title</td>




<td>price</td>




";




echo"</tr>\n";




//循环遍历




while($myrow=mysql_fetch_row($result)){




printf("<tr>




<td>%s</td>




<td>%s</td>




<td>%s</td>




<td>%s</td>",$myrow[0],$myrow[1],$myrow[2],$myrow[3]);




}




echo"</table>\n";




}




?>




<html>




<head>




<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>




<title>无标题文档</title>




</head>




<body>




<formaction=""name=""><br/>




<br/>




<inputtype="button"value="查询"name="submit"/>




<inputtype="text"name="checked"/>




</form>




</body>




</html>




=================9楼=====================

@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);

=================10楼=====================

<inputtype="submit"value="查询"name="submit"/>

=================11楼=====================

@jordan102我试了下@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);但是点了按钮还是不行。

=================12楼=====================

这句<inputtype="submit"value="查询"name="submit"/>我试了,但是我输入books的isbn到输入框的时候,点击查询,还是查不到isbn=1的对应的全部内容。

=================13楼=====================

只出来一部分数据,那你检查下没查出来的数据哪里不同。

=================14楼=====================

$result=mysql_query("select*frombooks",$db);

条件都不加,还算是条件查询吗?

=================15楼=====================

我运行之后没有数据表显示,显示结果如下:

会不会是那些字段没对应呢?

=================16楼=====================

回复xuzuning,不好意思!忘了加。

=================17楼=====================

没有符合条件的记录,自然列表就是空的

=================18楼=====================

<formaction=""name=""method="post"><br/>

=================19楼=====================

回复Jordan120,还是没有表显示。

=================20楼=====================

回复xuzuning,请问这句不是@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);通过查询isbn的编号然后判断符合条件的来输出表。

=================21楼=====================

表头也没有显示吗?

=================22楼=====================

回复Jordan02:没有显示

=================23楼=====================

现在的代码是如何写的,贴出来看看。

=================24楼=====================

<?php




//获得连接




$db=mysql_connect("localhost","root","root")ordie(mysql_error());




//echo"ConnectedtoMySQL<br/>";




//连接数据库




mysql_select_db("test")ordie(mysql_error());




//echo"ConnectedtoDatabase";









//查询数据,并用表格显示出来




// @$checked=$_POST['checked'];




// @$result=mysql_query("select*frombooks",$db);




// @$result=mysql_query("select*frombookswhereisbn='$checked'");




if(isset($_POST['checked'])){




@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);




echo"<tableborder=1>\n";




echo"<tr>




<td>isbn</td>




<td>author</td>




<td>title</td>




<td>price</td>




";




echo"</tr>\n";




//循环遍历




while($myrow=mysql_fetch_row($result)){




printf("<tr>




<td>%s</td>




<td>%s</td>




<td>%s</td>




<td>%s</td>",$myrow[0],$myrow[1],$myrow[2],$myrow[3]);




}




echo"</table>\n";




}




?>




<html>




<head>




<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>




<title>无标题文档</title>




</head>




<body>




<formaction=""name="post"><br/>




<br/>




<inputtype="submit"value="查询"name="submit"/>




<inputtype="text"name="checked"/>




</form>




</body>




</html>




=================25楼=====================

@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);

在哪里赋值的???

=================26楼=====================

嗦嘎,我在#18标红了你还抄错。

<formaction=""method="post">

=================27楼=====================

<?php

//获得连接

$db=mysql_connect("localhost","root","rDSzPnhnDJCAHYuj")ordie(mysql_error());

//echo"ConnectedtoMySQL<br/>";

//连接数据库

mysql_select_db("test")ordie(mysql_error());

//echo"ConnectedtoDatabase";

//查询数据,并用表格显示出来

//@$checked=$_POST['checked'];

//@$result=mysql_query("select*frombooks",$db);

//@$result=mysql_query("select*frombookswhereisbn='$checked'");

if(isset($_POST['checked'])){

$checked=$_POST['checked'];

@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);

echo"<tableborder=1>\n";

echo"<tr>

<td>isbn</td>

<td>author</td>

<td>title</td>

<td>price</td>

";

echo"</tr>\n";

//循环遍历

while($myrow=mysql_fetch_row($result)){

printf("<tr>

<td>%s</td>

<td>%s</td>

<td>%s</td>

<td>%s</td>",$myrow[0],$myrow[1],$myrow[2],$myrow[3]);

}

echo"</table>\n";

}

?>

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>

<title>无标题文档</title>

</head>

<body>

<formaction=""name="post"method="post"><br/>

<br/>

<inputtype="submit"value="查询"name="submit"/>

<inputtype="text"name="checked"/>

</form>

</body>

</html>

=================28楼=====================

<?php

//获得连接

$db=mysql_connect("localhost","root","root")ordie(mysql_error());

//echo"ConnectedtoMySQL<br/>";

//连接数据库

mysql_select_db("test")ordie(mysql_error());

//echo"ConnectedtoDatabase";

//查询数据,并用表格显示出来

//@$checked=$_POST['checked'];

//@$result=mysql_query("select*frombooks",$db);

//@$result=mysql_query("select*frombookswhereisbn='$checked'");

if(isset($_POST['checked'])){

$checked=$_POST['checked'];

@$result=mysql_query("select*frombookswhereisbn='$checked'",$db);

echo"<tableborder=1>\n";

echo"<tr>

<td>isbn</td>

<td>author</td>

<td>title</td>

<td>price</td>

";

echo"</tr>\n";

//循环遍历

while($myrow=mysql_fetch_row($result)){

printf("<tr>

<td>%s</td>

<td>%s</td>

<td>%s</td>

<td>%s</td>",$myrow[0],$myrow[1],$myrow[2],$myrow[3]);

}

echo"</table>\n";

}

?>

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>

<title>无标题文档</title>

</head>

<body>

<formaction=""name="post"method="post"><br/>

<br/>

<inputtype="submit"value="查询"name="submit"/>

<inputtype="text"name="checked"/>

</form>

</body>

</html>

=================29楼=====================

回复qxhaidao、jordan102:折腾了两位一个下午,qxhaidao的代码运行成功,jordan那个只显示了表头。我会给两位加分的,实在谢谢了!

=================30楼=====================

还在吗?我的弄出来怎么是这个啊?这个对吗?

如果您还有更好的解决方法,请在最下面评论中留下您的解决方法

(转载请注明出处和本文链接)

+1

0人

0 0