douxian5963 2019-04-14 18:28
浏览 369
已采纳

mysqli_fetch_array()期望参数1为mysqli_result,给出PHP论坛尝试的数组

I am trying to get and display two values from the database, 'cat_name', and 'cat_description' and display those in a table. A far as I can tell the code is fine. I am pretty new to PHP and have little experience with it.

I have tried going through many questions, and tried many possible answers. I cant really tell how many attempts I have tried to fix this issue.

    $query = $con->prepare("SELECT cat_id, cat_name, cat_description FROM categories");

    $query->execute();
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    if(!isset($result))
    {
        //something went wrong, display the error
        echo 'Error'; 
    }
    else
    {
        if($result == 0)
        {
            echo 'No categories defined yet.';
        }
        else
        {
            echo '<table border ="1">
                <tr>
                    <th>Category</th>
                    <th>Last Topic</th>
                </tr>';

            var_dump($result);

            while($row = mysqli_fetch_array($result)){
            echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';
                echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
            echo '</td>';
            echo '</tr>';
            }
        }

    }

I am supposed to expect a table showing two columns one showing Categories (hyperlinked) with a small description of the category taking from the database. And another column for topics within that category but have not been able to get that far as of yet.

Expected outcome:

Expected outcome

When I run this code all I seem to achieve is an error.

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given

  • 写回答

1条回答 默认 最新

  • doxd96148 2019-04-14 18:42
    关注

    Once you have run

    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    

    $result will be an array of the records from the query, so when you get to

    while($row = mysqli_fetch_array($result)){
    

    you already have the data so $result is not a result set, but an array of data. So instead you can replace this line with...

    foreach ( $result as $row ) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀