从phpmyadmin中检索数据

时间:2016-03-10 07:28:02

标签: php

我写了这段代码来从phpmyadmin中检索数据。但是当我在浏览器中加载页面“retrieve.php”时,所有结果都显示在没有任何搜索的情况下。后来当我按特定名称搜索时,那些字段只去了我搜索的那个字段。我可以知道当我没有进行任何搜索时,我如何摆脱显示器?基本上我想要做的是当我加载页面“retrieve.php”时,在我搜索任何特定名称之前,不会显示数据库中的任何字段。

<?php
 echo "<body style='background-color:gray'>";
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
	or die ( "uable to connect to MYSQL database" );
mysql_select_db( $project );


$sql= "SELECT * FROM registration ";


$query=mysql_query($sql) or die(mysql_error());

if (isset($_POST['search'])) {
	
	$search_term = mysql_real_escape_string($_POST['search_box']);
	
	$sql .= "WHERE first_name= '{$search_term}'";
	
	$sql .= " OR last_name= '{$search_term}'";

}


$query=mysql_query($sql) or die(mysql_error());

?>



<html>  
<head>  
<title>jon</title>  
</head>  
<body>  
<form name="search_form" method="POST" action="retrieve.php">  
<table width="599" border="1">  
<tr>  
<th>Search

<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">
  
</tr>  
</table>  
</form>  


<table width="600" border="1">  
<tr>  
<th width="91"> <div align="center">First Name </div></th>  
<th width="98"> <div align="center">Last Name </div></th>  
<th width="198"> <div align="center">Email </div></th>  
<th width="97"> <div align="center">City </div></th>  
<th width="59"> <div align="center">Country </div></th>  	
	
<tr>


<?php  while ($row=mysql_fetch_array($query)){ ?>


<tr>
	<td><?php echo $row['first_name'];?></td>
	<td><?php echo $row['last_name'];?></td>
	<td><?php echo $row['email'];?></td>
	<td><?php echo $row['address_city'];?></td>
    <td><?php echo $row['address_country'];?></td>
	
	
	
<tr>
 
<?php } ?>

</table>

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

<?php
 echo "<body style='background-color:gray'>";
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
    or die ( "uable to connect to MYSQL database" );
mysql_select_db( $project );


if (isset($_POST['search'])) {
    $sql= "SELECT * FROM registration ";

    $search_term = mysql_real_escape_string($_POST['search_box']);

    $sql .= "WHERE first_name= '{$search_term}'";

    $sql .= " OR last_name= '{$search_term}'";
    $query=mysql_query($sql) or die(mysql_error());
}



?>



<html>  
<head>  
<title>jon</title>  
</head>  
<body>  
<form name="search_form" method="POST" action="retrieve.php">  
<table width="599" border="1">  
<tr>  
<th>Search

<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">

</tr>  
</table>  
</form>  


<table width="600" border="1">  
<tr>  
<th width="91"> <div align="center">First Name </div></th>  
<th width="98"> <div align="center">Last Name </div></th>  
<th width="198"> <div align="center">Email </div></th>  
<th width="97"> <div align="center">City </div></th>  
<th width="59"> <div align="center">Country </div></th>     

<tr>


<?php  if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>


<tr>
    <td><?php echo $row['first_name'];?></td>
    <td><?php echo $row['last_name'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><?php echo $row['address_city'];?></td>
    <td><?php echo $row['address_country'];?></td>



<tr>

<?php }} ?>

</table>

仅在发布结果时才进行查询。当你有时间缩短MySQL_ *时,确保交换到MySQLi_ *。

相关问题