doucheng3811 2018-06-25 17:37
浏览 34
已采纳

为什么在尝试从此代码中获取数据时出错?

It says the line of it has an error like mysqli_num_rows() expects parameter 1 to be mysqli_result,it points to this line $resultCheck = mysqli_num_rows($result);

My code is:

<?php

$sql = "SELECT FROM videos ORDER BY video_id ASC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) { ?>
<li class="jp-playlist-current"><div><a href="javascript:;" class="jp- 
playlist-item-remove" style="display: none;">×</a><a href="javascript:;" 
class="jp-playlist-item jp-playlist-current" tabindex="0">1. <?php echo 
$row['video_name']; ?> <span class="jp-artist">by <?php echo 
$row['video_artist']; ?></span></a></div></li>

<?php   }

} else {
    echo "there are no songs!";
}

?>
  • 写回答

2条回答 默认 最新

  • doupang9614 2018-06-25 18:51
    关注

    mysqli_num_rows() function expects a mysqli_result object as it's parameter.

    It can fail if there were any error on the query and no mysqli_result object returned. A query can fail, if there ware syntax error, or wrong tablename-fieldnames used in it and also the important one is that the DB connection is active.

    You need to check them all.

    If the DB connection is Ok, done and checked previously, then you don't need the following code:

    <?php
    $con = mysqli_connect("{DB_HOST}", "{db_user}","{db_password}","{db_name}");
    if (mysqli_connect_errno()) die("DB Connect Error: " . mysqli_connect_error());
    ?>
    

    Then please check if the query successful too before trying to fetch result from it (mysqli_query() function returns false if the query fails).

    <?php
    $result = mysqli_query($con, "SELECT * FROM videos ORDER BY video_id ASC LIMIT 2,1");
    if (!$result) die('Error on the query...');
    $resultCheck = mysqli_num_rows($result);
    
    if ($resultCheck) {
        while ($row = mysqli_fetch_assoc($result)) { ?>
    ...<?php echo $row['video_name']; ?>...
    ...<?php echo $row['video_artist']; ?>...
    <?php   }
    
    } else {
        echo "there are no songs!";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号