2016-11-28 21 views
0

我是一個一流的執行語句mysql和我不知道如何打印返回數組變量如何在php中的表中打印行數組?

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($行)我verefy其從數據庫返回的聲明

$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 !"; 
} 

我在哪裏錯了?

+1

'mysql_fetch_array'不工作。 – chris85

+0

我明白了,沒錯 –

回答

0

你混合mysql_函數與mysqli_功能

更改您的取得與`mysqli`到

$r = mysqli_fetch_array() 
+0

我試着像你說的,但沒有 –