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

时间:2022-09-06 20:46:54

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.

我是oop风格的新手。我从上周开始练习它,我制作简单的CRUD网站。但是当我尝试从mysql db中获取行时,我遇到了一个问题,它始终显示1行。

my created a class named class.user.php

我创建了一个名为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\n", 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 个解决方案

#1


0  

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.

然后你的$ datArray中包含所有行。

#2


0  

This is because you are appending to the result array only when $current_row == 1.

这是因为仅在$ current_row == 1时才附加到结果数组。

Try changing your while loop like this:

尝试更改你的while循环,如下所示:

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

Also, you are not closing the db connection correctly, you are mixing OOP style with procedural mysqli functions. This is how it should be:

此外,您没有正确关闭数据库连接,您正在将OOP样式与过程mysqli函数混合。这应该是这样的:

$this->db->close();
$this->db = null;

Finally, you should return the data array after you closed the connection. If you returned the data array first and closed the connection after, that code wont get executed.

最后,在关闭连接后应返回数据数组。如果您先返回数据数组并关闭连接后,该代码将不会执行。

So your last three lines should look like this:

所以你的最后三行应该是这样的:

$this->db->close();
$this->db = null;
return $dataArray;

#1


0  

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.

然后你的$ datArray中包含所有行。

#2


0  

This is because you are appending to the result array only when $current_row == 1.

这是因为仅在$ current_row == 1时才附加到结果数组。

Try changing your while loop like this:

尝试更改你的while循环,如下所示:

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

Also, you are not closing the db connection correctly, you are mixing OOP style with procedural mysqli functions. This is how it should be:

此外,您没有正确关闭数据库连接,您正在将OOP样式与过程mysqli函数混合。这应该是这样的:

$this->db->close();
$this->db = null;

Finally, you should return the data array after you closed the connection. If you returned the data array first and closed the connection after, that code wont get executed.

最后,在关闭连接后应返回数据数组。如果您先返回数据数组并关闭连接后,该代码将不会执行。

So your last three lines should look like this:

所以你的最后三行应该是这样的:

$this->db->close();
$this->db = null;
return $dataArray;