Reputation:
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
Upvotes: 0
Views: 7345
Reputation: 1035
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.
Upvotes: 1