0

好的,到目前为止,我的代码运行良好,一切顺利,我唯一的问题是,当我尝试打印无序列表及其内容时,我什么也得不到。当我查看我的源代码时,我有<ul> </ul>. 有一个空间,所以肯定会发生一些事情。

这是我的代码,我稍微评论了一下,但发生的事情很明显:

$uname = mysqli_real_escape_string($link, $_SESSION['Username']); //Get username ready
$sql = mysqli_query($link, "SELECT * FROM users WHERE Username = '" . $uname . "'"); //SQL Query result

    if(!$sql)
        {
        echo "Error retrieving User ID. Please try again. MySQL Error: " . mysqli_error($link);
        }
    elseif($row = mysqli_fetch_assoc($sql))
        {
            $uid = $row['UserID']; //Obtain UserID
        }
    else
        {
        echo "Error: " . mysqli_error($link) . "<br />" . $uname . " / " . $sql . " / " . $uid;
        }

    mysqli_free_result($sql);

    $sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table

    if(!$sql)
        {
        echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
        }
    elseif($row = mysqli_fetch_assoc($sql))
        {
        if(mysqli_num_rows($sql)==0)
            {
            echo "Sorry, there are currently no open auditions. Please try back at a later date.";
            }
        else
            {
            echo "<ul>";
            while($row = mysqli_fetch_assoc($sql))
                {
                echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
                }
            echo "</ul>";
            }
        }
    else
        {
        echo "Error: " . mysqli_error($link);
        }

我哪里错了?它唯一没有做的就是实际获取任何结果,并且我已将一些数据放入表中,因此有条目!否则会说没有。我已经扭转了这一点,因此如果没有 0 个条目并且有效,它会显示消息。我做错了什么伙计们?

提前致谢。

4

1 回答 1

0

您正在获取结果两次。相反,只在while循环中获取结果:

<?php
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table

    if(!$sql)
        {
            echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
        }
        else{
            if(mysqli_num_rows($sql)==0)
            {
                echo "Sorry, there are currently no open auditions. Please try back at a later date.";
            }
            else
            {
                echo "<ul>";
                while($row = mysqli_fetch_assoc($sql))
                {
                    echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
                }
                echo "</ul>";
            }
        }

?>

有关 mysql_fetch_assoc 的更多信息,请参阅此链接

于 2013-04-18T00:15:34.417 回答