else 语句中的 while 循环不打印所有答案
While-loop inside else-statement not printing all answers
我得到了这段代码,它链接到我的索引页上的搜索字段:
<?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
循环一切正常,但是因为一些搜索词(例如 'car')应该同时打印 Project CARS 和 Rise of Incarnates,我需要一个 while
-循环。
我也尝试将 while
循环放在 if
语句之前,但这也不起作用。我做错了什么?
我已对您的代码进行了一些更正。请使用以下代码重新验证
我已经使用我的数据库表尝试了代码,它显示了正确的值...
<?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
(即忽略大写字母和小写字母),请告诉我。我会更新代码
我得到了这段代码,它链接到我的索引页上的搜索字段:
<?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
循环一切正常,但是因为一些搜索词(例如 'car')应该同时打印 Project CARS 和 Rise of Incarnates,我需要一个 while
-循环。
我也尝试将 while
循环放在 if
语句之前,但这也不起作用。我做错了什么?
我已对您的代码进行了一些更正。请使用以下代码重新验证
我已经使用我的数据库表尝试了代码,它显示了正确的值...
<?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
(即忽略大写字母和小写字母),请告诉我。我会更新代码