dsbiw2911188 2013-07-17 16:21
浏览 56
已采纳

`while`没有显示回声

I'm having trouble with getting my echo showing up. i think there is a problem with the while but the table is in the mysql so it should work normally. This is my code

    <?php


$sql = $db->prepare('SELECT
            topic_id,
            topic_subject
        FROM
            topics
        WHERE
            topics.topic_id = :topid');
$sql->bindParam(':topid', $_GET['id'], PDO::PARAM_INT); 
$sql->execute();
$result = $sql->rowCount();



        if($result === FALSE){
            echo 'The topic could not be displayed, please try again later.';
        }
        elseif(count($result) === 0){
            echo 'This topic doesn&prime;t exist.';
        }
    else
        {
        while($row = $sql->fetch())
        {
            //display post data
            echo '<table class="topic" border="1">
                    <tr>
                        <th colspan="2">' . $row['topic_subject'] . '</th>
                    </tr>'; ?>

the while should show up because the topic exists in mysql. When i'm using avar_dump($sql->errorInfo()); it say's array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } The null is because i created the topic in mysql as a test.

  • 写回答

2条回答 默认 最新

  • duanluanlang8501 2013-07-17 16:28
    关注
    $result = $sql->rowCount();
    
    elseif(count($result) === 0){
    

    $result is being assigned a row count; but count() is designed to count the elements in an array. From the manual

     If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned. 
    

    I think all you need is:

    elseif($result === 0){
    

    What I think's happening is that there are no results from your query, but the call to count() isn't working. It's passing that check, and since there are no records to retrieve, it's not going into the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?