[英]Retrieve data from phpmyadmin
I wrote this code to retrieve data from phpmyadmin. 我写了这段代码来从phpmyadmin检索数据。 But when the i load the page "retrieve.php" in my browser then all the result show up without any search.
但是,当我在浏览器中加载页面“ retrieve.php”时,所有结果都会显示出来,而无需任何搜索。 Later when i do search by specific name then those field go and only the one that i searched for show.
后来,当我按特定名称搜索时,这些字段就会显示,并且只显示我搜索的那个字段。 May i know how do i get rid of the display that i get when i am not doing any search ?
我可以知道如何不进行任何搜索时摆脱显示的显示吗? Basically what i want to do is that when i load the page "retrieve.php" then no field from database should display until i search for any specific name.
基本上我想做的是,当我加载页面“ 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>
Try the following: 请尝试以下操作:
<?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>
Only do the query if there is a post result. 仅在有发布结果的情况下查询。 Make sure to swap to MySQLi_* when you have the time as MySQL_* is depreciated.
请确保在您有时间折旧MySQL_ *时交换到MySQLi_ *。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.