<?php
include 'connect.php';
$stfind = $_GET['startup_find'];
$sqlselect = "SELECT * from startup_schema where Name = '$stfind'";
$comments = mysqli_query($mysqli, $sqlselect);
$mysql_num_rows = mysqli_num_rows($comments);
while ($row = mysqli_fetch_array($comments)) {
if($mysql_num_rows > 0) {
echo "<table border='1' style='width: 100%'>";
echo "<tr>"; // LINE ADDED
echo "<th>ID</th>";
echo "<td>" . $row['ID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name</th>";
echo "<td>" . $row['Name'] ."</td>";
echo "</tr></table>";
} else {
echo "No Data Found.";
}
}
mysqli_close($mysqli);
?>
这是我的PHP! 我的connect.php与数据库有连接。 一切正常,直到if语句,但如果行不存在,程序不执行else语句。
答案 0 :(得分:2)
你应该在while循环之前检查条件(如果你没有行,那么循环就不会执行。)
if($mysql_num_rows > 0) {
while ($row = mysqli_fetch_array($comments)) {
echo "<table border='1' style='width: 100%'>";
echo "<tr>"; // LINE ADDED
echo "<th>ID</th>";
echo "<td>" . $row['ID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name</th>";
echo "<td>" . $row['Name'] ."</td>";
echo "</tr></table>";
}
} else {
echo "No Data Found.";
}
答案 1 :(得分:2)
只需将代码替换为给定的代码:
<?php
include 'connect.php';
$stfind = $_GET['startup_find'];
$sqlselect = "SELECT * from `startup_schema` where `Name` = '".$stfind."'";
$comments = mysqli_query($mysqli, $sqlselect);
$mysql_num_rows = mysqli_num_rows($comments);
if($mysql_num_rows > 0) {
while ($row = mysqli_fetch_array($comments)) {
echo "<table border='1' style='width: 100%'>";
echo "<tr>"; // LINE ADDED
echo "<th>ID</th>";
echo "<td>" . $row['ID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name</th>";
echo "<td>" . $row['Name'] ."</td>";
echo "</tr></table>";
}
}
else {
echo "No Data Found.";
}
mysqli_close($mysqli);
?>
实际上你在循环中使用if条件,这是错误的,我把它放在循环之前。
就是这样。 :)
答案 2 :(得分:0)
将if
和else
移出while
:
<?php
include 'connect.php';
$stfind = $_GET['startup_find'];
$sqlselect = "SELECT * from startup_schema where Name = '$stfind'";
$comments = mysqli_query($mysqli, $sqlselect);
$mysql_num_rows = mysqli_num_rows($comments);
if($mysql_num_rows > 0) {
while ($row = mysqli_fetch_array($comments)) {
echo "<table border='1' style='width: 100%'>";
echo "<tr>"; // LINE ADDED
echo "<th>ID</th>";
echo "<td>" . $row['ID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name</th>";
echo "<td>" . $row['Name'] ."</td>";
echo "</tr></table>";
}
} else {
echo "No Data Found.";
}
mysqli_close($mysqli);
?>
答案 3 :(得分:0)
mysql_fetch_array
会返回NULL
,因此如果没有行,则不会输入while
循环。你应该把支票放在它外面,而不是在里面:
if($mysql_num_rows > 0) {
while ($row = mysqli_fetch_array($comments)) {
echo "<table border='1' style='width: 100%'>";
echo "<tr>"; // LINE ADDED
echo "<th>ID</th>";
echo "<td>" . $row['ID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name</th>";
echo "<td>" . $row['Name'] ."</td>";
echo "</tr></table>";
}
} else {
echo "No Data Found.";
}
答案 4 :(得分:0)
您可以删除以下行
$comments = mysqli_query($mysqli, $sqlselect);
带
if($comments = mysqli_query($mysqli, $sqlselect)){
// do your tasks here
}
else{
echo mysqli_error($mysqli);
}
你的$ row = mysqli_fetch_array($ comments)部分应该进入if($ mysql_num_rows&gt; 0)块