php - 什么条件处理没有 MySQL 记录与 SQL 语句匹配的事件?

标签 php mysql if-statement conditional-statements

如果没有记录器满足 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/

相关文章:

bash - 如何检查数字是否在shell中的范围内

python - 根据条件更改函数定义

php - 如何在记录中显示其他数据 while 子句 where is null?

php - jQuery slideToggle 没有触发

php - Mysql、phpmyadmin、创建数据库

mysql - 如何在varchar字段中选择mysql中的最大值

java - 为什么加起来是 11 而不是 8?

php - 使用 PDO 登录后显示个人资料页面

php - 尝试修复从 MYSQL 收到的错误时遇到问题

mysql - "insert...where not exists"语句中的重复列名