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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器