如果没有记录器满足 MySQL 表属性“isVisible”= 的 SQL 语句,我打算让以下代码显示“h2”标签和文本(显示在代码底部的 else 语句中) = 1。
我决定测试一下查询是否返回“空”结果,这一定是我的逻辑有缺陷的地方。即使没有记录与 SQL 语句的“WHERE isVisible = 1”部分匹配,我的查询结果仍必须返回 null 以外的值。
我在数据库中只有一条记录硬编码为“0”。当“isVisible”属性返回值“1”时,代码将正确执行。异常(exception)是唯一给我带来麻烦的事情。我通常不怎么使用 PHP,所以我确信我可能只是在下面几行的第一个 if 语句中测试了错误的条件。
感谢您的时间和帮助。
<?php
// Generate SQL Statements
$sql_position = "SELECT * FROM tbl_experience
WHERE isVisible = 1
ORDER BY 'displayPosition'";
// Run Query
$result = mysql_query($sql_position);
// Iteration Variable
$i = 0;
if ($result != null) {
// List all of the experiences as a timeline
while($row = mysql_fetch_array($result)) {
// Build a div to contain the experience properties
echo "<div id='experienceContainer_" . $row['displayPosition'] . "' class='experienceContainer";
if (i % 2) {
// The row should be displayed with 'Even' class attributes
echo " Even";
} else {
// The row should be displayed with 'Odd' class attributes
echo " Odd";
}
// Finish the opening <div> tag
echo "'>";
// Display the attributes of the experience
// Experience Type
echo "<div id='type_" . $row['displayPosition'] . "' class='experienceType'>";
switch ($row['type']) {
case 0:
echo "Undefined";
break;
case 1:
echo "Work";
break;
case 2:
echo "Achievement";
break;
case 3:
echo "Award";
break;
}
echo "</div>";
// Experience Title
echo "<div id='title_" . $row['displayPosition'] . "' class='experienceTitle'>";
echo $row['title'];
echo "</div>";
// Experience Description
echo "<div id='description_" . $row['displayPosition'] . "' class='experienceDescription'>";
echo $row['description'];
echo "</div>";
// Close the 'experienceContainer' div
echo "</div><!-- end of experienceContainer_" . $row['displayPosition'] . " -->";
}
} else {
// There are no visible records
echo "<h2>No entries for \"Experience\" can be found</h2>";
}
最佳答案
您需要使用mysql_num_rows
if (mysql_num_rows($result) > 0) {
当没有可显示的行时,此 if 条件将保证您将被路由到 else
条件。
关于php - 什么条件处理没有 MySQL 记录与 SQL 语句匹配的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297925/