Anwarul Islam Abir
Anwarul Islam Abir

Reputation: 23

Using Php mysql query do not get data from php class, though there are data

class book {

function __construct() {
$conn = mysql_connect('localhost', 'root', '') or die('can nit connect to DB');
mysql_select_db('atomic_project', $conn) or die('can not connect to db');
}

public function listView() {
    $allData = array();
    $query = "SELECT * FROM `book`";
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        print_r($row);
        $allData [] = $row;
    }
    return $allData;
}

}

My index page

    $listViewObj = new book();
    $allData = $listViewObj->listView();

    echo "<pre>";
    print_r($allData);
    echo "<pre>";

Here is my code and the table here is my table data , i can insert data into table but no row is found i can't understand why no data is shown, please help me

enter image description here

Upvotes: 0

Views: 46

Answers (1)

Aman Kumar
Aman Kumar

Reputation: 4557

    <?php
class book {

public function listView() {
    $conn = mysql_connect('localhost', 'root', '') or die('can nit connect to DB');
    mysql_select_db('atomic_project', $conn) or die('can not connect to db');

    $allData = array();
    $query = "SELECT * FROM `book`";
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        //echo "<pre>";
        //print_r($row);
        $allData [] = $row;
    }
   return $allData;
}
}
book::listView(); //scope resolution operator 
?>

or calling function by object

$obj = new book();
$obj->listView();

Upvotes: 1

Related Questions