PHP ADODB GetRows

From CoderLearner
Jump to: navigation, search

Create Database And Table For the Tutorial

mysql> create database mytestdatabase;
Query OK, 1 row affected (0.00 sec)
mysql> use mytestdatabase;
Database changed
mysql> CREATE TABLE profile
    -> (
    -> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> name VARCHAR(200) NOT NULL,
    -> age INT NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)

GetRows()

GetRows() to return all records.

<?php

       include 'adodb.inc.php';

       $host = 'localhost';
       $user = 'root';
       $pass = 'caojiang';
       $dbname = 'w3cyberlearnings';

       $conn1 = &ADONewConnection('mysql');
       $conn1->PConnect($host, $user, $pass, $dbname);

       $sql = "SELECT * FROM profile";

       $result =  $conn1->Execute($sql);
       if ($result==false) {
          print 'error' . $conn1->ErrorMsg() . '<br>';
       } else {
          
          print_r($result->GetRows());
          
       }

?>

Return

Array
(
    [0] => Array
        (
            [0] => 1
            [id] => 1
            [1] => Bob
            [name] => Bob
            [2] => 24
            [age] => 24
        )

    [1] => Array
        (
            [0] => 2
            [id] => 2
            [1] => Janny
            [name] => Janny
            [2] => 24
            [age] => 24
        )

    [2] => Array
        (
            [0] => 3
            [id] => 3
            [1] => Sopka
            [name] => Sopka
            [2] => 24
            [age] => 24
        )

    [3] => Array
        (
            [0] => 4
            [id] => 4
            [1] => Jinny
            [name] => Jinny
            [2] => 28
            [age] => 28
        )

    [4] => Array
        (
            [0] => 5
            [id] => 5
            [1] => Horny
            [name] => Horny
            [2] => 24
            [age] => 24
        )

    [5] => Array
        (
            [0] => 6
            [id] => 6
            [1] => Iinny
            [name] => Iinny
            [2] => 28
            [age] => 28
        )

)