0

如果没有符合 MySQL 表属性“isVisible”== 1 的 SQL 语句的记录器,我打算让以下代码显示“h2”标记和文本(显示在代码底部的 else 语句中)。

我决定测试一下查询是否返回“null”结果,这一定是我的逻辑存在缺陷的地方。即使没有任何记录与 SQL 语句的“WHERE isVisible = 1”部分匹配,我的查询结果仍必须返回 null 以外的值。

我在数据库中只有一条记录硬编码为“0”。当“isVisible”属性返回值为“1”时,代码将正确执行。例外是唯一给我带来麻烦的事情。我通常不经常使用 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>";

            }
4

1 回答 1

0

你需要使用mysql_num_rows

if (mysql_num_rows($result) > 0) {

这个 if 条件将保证else当没有要显示的行时,您将被路由到该条件。

于 2013-08-18T10:10:15.040 回答