SQL查询成功但没有信息

时间:2013-04-18 00:11:58

标签: php mysql mysqli

好的,所以到目前为止我的代码工作得非常好,一切都通过,我唯一的问题是,当我尝试打印无序列表时,它的内容我什么也得不到。当我查看我的源代码时,我有<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个条目并且有效,它会显示消息。我做错了什么人?

提前致谢。

1 个答案:

答案 0 :(得分: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>";
            }
        }

?>

See this link for more information regarding mysql_fetch_assoc