我收到了这段代码,该代码链接到索引页面上的搜索字段:
<?php
ini_set('display_errors', 2);
$search = $_GET ['q'];
$conn = mysqli_connect("localhost", "root", "","release");
$query = mysqli_query($conn,"SELECT * FROM game WHERE game_name LIKE '%". $search ."%'");
$foundnum = mysqli_fetch_assoc($query);
$count = count($foundnum['game_name']);
if ($foundnum == 0) {
echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
}
else {
while($foundnum= mysqli_fetch_assoc($query))
{
echo "$count result(s) found!<p>";
echo"<pre/>";print_r($foundnum['game_name']);
echo"<pre/>";print_r($foundnum['game_release']);
}
}
?>
在没有while
循环的情况下,一切正常,但由于某些搜索字词(例如“汽车”)应该同时打印“项目CARS”和“崛起的化身”,我需要一个while
- 循环。
我也尝试将while
- 循环放在if
- 语句之前,但这也不起作用。我做错了什么?
答案 0 :(得分:1)
我已在您的代码中进行了一些更正..请使用以下代码重新进行修改
我已经使用我的数据库表尝试了代码,它显示了正确的值......
<?php
ini_set('display_errors', 1);
$search = $_GET['q'];
$conn = mysqli_connect("localhost", "root", "", "release");
$query = mysqli_query($conn, "SELECT * FROM game WHERE game_name LIKE '%" . $search . "%'");
$count = mysqli_num_rows($query); // right way to find row count
if ($count == 0)
{
echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
}
else
{
while ($foundnum = mysqli_fetch_assoc($query))
{
echo "$count result(s) found!<p>";
echo"<pre>";
print_r($foundnum['game_name']);
echo"</pre><pre>";
print_r($foundnum['game_code']);
echo"</pre>";
}
}
?>
如果你想搜索insensitively
(即忽略大写和小写字母)而不是让我知道..我会更新代码