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条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像