dongpin3794 2014-10-13 11:30
浏览 64
已采纳

如何使用php oop从mysql显示多行?

I'm a newbie in oop style. I start practicing it since last week and I make simple CRUD website. but i got a problem when i tried fetching rows from mysql db its always display 1 row.

my created a class named class.user.php

and it shows here:

include "db_config.php";

class User{


    private $db;
    public function __construct(){  
        $this->connect();

        }
        private function connect($db_connect=true){ 
            if ($db_connect){
                $this->db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
                if(mysqli_connect_errno()) {        
                    printf("DB Connect failed: %s
", mysqli_connect_error());
                    exit;
            }
        }       
    }
    public function get_tutoriallist(){
        $db = $this->db;
        if(empty($db)){
            $this->connect();
            $db = $this->db;
        }
        try {

            $stmt = $this->db->prepare('SELECT * FROM `topic`');
            $stmt->execute();
            $result = $stmt->get_result();
            $dataArray = array();
            while($row = $result->fetch_assoc()){
                $count_row = $result->num_rows;
                if ($count_row == 1) {
                $dataArray[] = $row;
                }
            }
            return ($dataArray);

            mysqli_close($db);
            $this->db = null;   
        } catch (Exception $e) {
            echo $e->errorMessage();
        }

    }
}

and i call it using this:

$data = $user->get_tutoriallist(); 
                        if (!empty($data)) {
                            foreach ($data as $row){    
                                echo "<tr>";    
                                echo"<td>".$row['category']."</td>";
                                echo"<td>".$row['title']."</td>";
                                echo"<td>".$row['detail']."</td>";
                                echo"<td>".$row['photo']."</td>";
                                echo"<td>".$row['video_link']."</td>";
                                echo"<td>".$row['date_post']."</td>";
                                echo"<td class='option'><center><a href ='#' class='edit'>Edit</a>
                                    &nbsp;<a href='#'>Delete</a></center></td>";                                    
                                echo "</tr>";
                            }
                        }else{
                                echo '<tr><td colspan="6"><center><h2>No entries</h2></center></td></tr>';
                        }
  • 写回答

2条回答 默认 最新

  • doutao8774 2014-10-13 11:34
    关注

    I'm not quite sure what is going on here:

            while($row = $result->fetch_assoc()){
                $count_row = $result->num_rows;
                if ($count_row == 1) {
                $dataArray[] = $row;
                }
    

    But normally, you just iterate through the results and append them to an array:

            while($row = $result->fetch_assoc()){
                $dataArray[] = $row;
                }
    

    Then your $datArray has all the rows in it.

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

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复