0
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

9
  • Where is your class object and where is you calling this function. Please share your full code? Commented Mar 12, 2016 at 11:42
  • $book_show=new book(); and then $book_show->listView(); use this after your code. Commented Mar 12, 2016 at 11:44
  • what error r u getting?? Commented Mar 12, 2016 at 11:47
  • what is your database password??? Commented Mar 12, 2016 at 11:53
  • and y r u asking for db password, OP using localhost. @WisdmLabs Commented Mar 12, 2016 at 11:55

1 Answer 1

1
    <?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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.