0

I'm a class execute statement mysql and I don't know how to print a return array variable

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;
    }
}

This when i call the method to execute select statement the rows return an array, but i don't kno how to print... If i do this printr($rows) I verefy that the statement its returning from database

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

Where am I wrong?

Keyur
  • 1,113
  • 1
  • 23
  • 42

1 Answers1

0

You are mixing mysql_ functions with mysqli_ functions

Change your fetch to

$r = mysqli_fetch_array()
Arun
  • 1,609
  • 1
  • 15
  • 18