我需要在我的数据库中返回用户的搜索结果。
目前,我已经设法在 MySQL 查询中使用 LIKE 函数来返回结果计数,但我需要它来回显所有结果的用户名,而不仅仅是第一个结果:
<?php
// mysql details
$host="localhost"; // Host name
$username="classified"; // Mysql username
$password="classified"; // Mysql password
$db_name="jaycraftcommunity"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// form variable to local
$search = $_POST['username'];
// make query
//$sql = "SELECT * FROM $tbl_name WHERE (username='$search' OR verified='$search')"; // sql query
$sql = "SELECT username FROM $tbl_name WHERE username LIKE '%$search%'"; // sql query
$result = mysql_query($sql); // do query
// handle query data
$count = mysql_num_rows($result);
$row = mysql_fetch_row($result);
$array = mysql_fetch_array($result);
echo $count;
foreach( $row as $arrays ): ?>
<tr>
<td><?php echo htmlspecialchars( $arrays ); ?></td>
<br />
</tr>
<?php
endforeach;
?>
HTML 表单如下所示:
<h2>Search</h2>
<form method="post" action="search_engine.php">
Search for:
<input type="text" name="username">
<input type="submit" value="Search for username">
</form>
目前,如果您访问http://dev.jaycraft.co/player/dupe/search.php,您可以看到它正在运行。搜索单个字母,它显示 echo $count 有多少结果,但只回显第一个结果。
谢谢