【问题标题】:How can I print a rows mysql array in a table in php? [duplicate]如何在 php 的表中打印行 mysql 数组? [复制]
【发布时间】:2016-11-28 04:52:12
【问题描述】:

我是一个类执行语句mysql,不知道如何打印返回数组变量

class Db {
    // The database connection
    protected static $connection;

    public function connect() {    
        // Try and connect to the database
        if(!isset(self::$connection)) {
            self::$connection = new mysqli('localhost','root','123456','dbGanaderia');
        }

        // If connection was not successful, handle the error
        if(self::$connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return false;
        }
        return self::$connection;
    }

    public function query($query) {
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $result = $connection -> query($query);

        return $result;
    }

    public function select($query) {
        $rows = array();
        $result = $this -> query($query);
        if($result === false) {
            return false;
        }
        while ($row = $result -> fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }
}

当我调用方法来执行 select 语句时,行返回一个数组,但我不知道如何打印...如果我这样做 printr($rows) 我确认该语句从数据库返回

$db = new Db();  

$rows = $db -> select("SELECT  *FROM user");

if ($rows == true) { 
    echo "<table border = '1'> \n"; 
    echo "<tr><td>name</td></tr> \n"; 
    while($row = mysql_fetch_array($rows)) { 
        echo "<tr><td>".$row['1']."</td></tr> \n"; 
    } 
    echo "</table> \n"; 
} else { 
    echo "¡ No data !"; 
}

我哪里错了?

【问题讨论】:

  • mysql_fetch_array 不适用于mysqli
  • 我明白了,没错

标签: php mysql arrays mysqli


【解决方案1】:

你正在混合 mysql_ 函数和 mysqli_ 函数

将您的抓取更改为

$r = mysqli_fetch_array()

【讨论】:

  • 我按你说的试过了,但没有
猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 2021-02-13
  • 2019-12-06
  • 2022-01-10
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多