0

Good day everyone. I am trying to run my query but everytime I try to run it, it would give me this kind of error: Call to a member function fetchAll() on a non-object. I don't really grab the whole idea on what is this problem all about and how it is caused.

Here is my code.

bookReserve.php

<?php
    session_start();
    include_once "../styles/header-menu-out.php";
    include_once "dbconnection.php";


    function __autoload($class){
        include_once("../main/".$class.".php");}

        $code = new codex_books();

        $sname = $_POST['sname'];
        $sid = $_POST['sid'];
        $id = $_POST['id'];
        $title = $_POST['title'];
        $author = $_POST['author'];
        $isbn = $_POST['isbn'];
        $publisher = $_POST['publisher'];
        $language = $_POST['language'];
        $genre = $_POST['genre'];
        $quantity = $_POST['quantity'];
        $date_to_be_borrow = $_POST['date_to_be_borrow'];
        $statement = $code->bookreserve1($id,"book_info");

        if(isset($_POST['reserve']))
        {
            while($row = $statement->fetchAll(PDO::FETCH_ASSOC))
            {
                $oldstock=$row['quantity'];
            }

            $newstock = $oldstock-$quantity;
            $code->bookreserve2($newstock,"book_info");
            $code->bookreserve3($q,"reserve_list");
        }
        else
            echo "<script type='text/javascript'>
                    alert('Successfully Reserved.');
                    window.location='bookReservelist.php';
                </script>";

?>

codex_books.php

public function bookreserve1($id, $table)
{
    $q = "SELECT * FROM $table WHERE id = :id";
    $stmt = $this->con->prepare($q);
    $stmt->execute(array(':id'=>$id));
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result;
}

Can you help me solve this problem and make me understand this error in layman's term? I am new to this. Hope you can understand. Thanks

8
  • $code->bookreserve1($id,"book_info") is returning false. So the problem lies somewhere in bookreserve1 method.
    – Ivarpoiss
    Mar 16, 2014 at 1:07
  • var_dump($statement);. Mar 16, 2014 at 1:08
  • @web2students.com my mistake, I have posted the wrong lines of codes. Edited it already.
    – user3345570
    Mar 16, 2014 at 1:09
  • And If you're using fetchAll then you should use foreach($statement->fetchAll() as $row) or just while($row = $statement->fetch())
    – Ivarpoiss
    Mar 16, 2014 at 1:09
  • For your code to work you should return the $stmt object from bookreserve1. Or the array returned by $stmt->fetchAll(). Then later iterate over it using foreach($arr_book_info as $row)
    – Ivarpoiss
    Mar 16, 2014 at 1:12

1 Answer 1

1

Try this

public function bookreserve1($id, $table)
{
    $q = "SELECT * FROM $table WHERE id = :id";
    $stmt = $this->con->prepare($q);
    $stmt->execute(array(':id'=>$id));
    //fetchAll gets all rows found by query, as an array of rows.
    //In your code $result = $stmt->fetch() returned only the first row, as ann associative array.
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $result;
}

and

function __autoload($class) {include_once("../main/".$class.".php");}

$code = new codex_books();

$sname = $_POST['sname'];
$sid = $_POST['sid'];
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
$publisher = $_POST['publisher'];
$language = $_POST['language'];
$genre = $_POST['genre'];
$quantity = $_POST['quantity'];
$date_to_be_borrow = $_POST['date_to_be_borrow'];

$result = $code->bookreserve1($id,"book_info");

if(isset($_POST['reserve']))
{
    //Iterate over the array of rows.
    foreach($result as $row)
    {
        $oldstock=$row['quantity'];
    }

    $newstock = $oldstock-$quantity;

    $code->bookreserve2($newstock,"book_info");
    $code->bookreserve3($q,"reserve_list");
}
else {
echo "<script type='text/javascript'>alert('Successfully Reserved.');window.location='bookReservelist.php';</script>";
}
?>

In your code the $statement variable assigned with $code->bookreserve1 was the first row returned by $stmt->fetch which is an array type not a PDO statement object. That's why calling $statement->fetchAll() resulted in error. $statement was an array at that point.

3
  • Excellent! From what I have noticed from my code to yours, Is it a neccessity to have the same name from your oop code to your pdo code? like that $result?
    – user3345570
    Mar 16, 2014 at 1:39
  • From what you have edited, now I understand. Thanks sir
    – user3345570
    Mar 16, 2014 at 1:46
  • The variable name "result" is irrelevant. I just changed the name to something other than $statement since it's not a pdo statement, it's an array. $result in bookreserve1 method is entirely different variable in different scope. Just happens to have the same name (and value). Go look up what variable scopes are and how they work. You'll get used to it.
    – Ivarpoiss
    Mar 16, 2014 at 1:47

Your Answer

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