PDO Fetch and rowCount (PHP) [duplicate] - php

This question already has answers here:
Fatal error: Call to a member function rowCount() on a non-object
(2 answers)
Closed 3 years ago.
try {
$sql = "SELECT * FROM tbl4 WHERE tur=:tur AND link=:link";
$res = $db -> prepare($sql);
$res -> bindParam(":tur",$tur);
$res -> bindParam(":link",$link);
$res -> execute();
$res = $res -> fetch(PDO::FETCH_ASSOC);
if($res->rowCount()!=0){
echo 'OK';
}else{
echo "No!";
}
}catch (Exception $e) {
echo "Error5";
}
But the problem is I get that error
Uncaught Error: Call to a member function rowCount() on array in C:\xampp\htdocs\sf-19\pages\topic.php:19 Stack trace: #0 C:\xampp\htdocs\sf-19\index.php(18): require() #1 {main} thrown in C:\xampp\htdocs\sf-19\pages\topic.php on line 19
If I delete if statement (has rowCount) everything works well. Yet I want to check if the query has result.
What am I missing? or what is the best way to check if query has result?
Thanks

Your problem is this line:
$res = $res -> fetch(PDO::FETCH_ASSOC);
which is overwriting the value of $res before you use it in
if($res->rowCount()!=0){
You should probably use a different variable name for that value e.g.
$rows = $res -> fetch(PDO::FETCH_ASSOC);

Related

Does anyone know why am I getting a fetch_array() boolean error? [duplicate]

This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am trying to fetch TaxRate data from a table and I'm trying to select the first array only. However, I keep getting this error
"Fatal error: Uncaught Error: Call to a member function fetch_array()
on boolean in C:\xampp\htdocs\flowerique\shoppingCart.php:77 Stack
trace: #0 {main} thrown in C:\xampp\htdocs\flowerique\shoppingCart.php
on line 77"
Does anyone know the cause of this error and how do I fix it? Thanks!
This is my code :
//Retrieve Current GST Pricing
$qry = "SELECT * FROM gst GROUP BY EffectiveDate DESC";
$stmt = $conn->prepare($qry);
$stmt->execute();
//$stmt->close();
$result = $conn->query($qry);
$row = $result->fetch_array(); // This is line 77
while($row["EffectiveDate"] < date("Y-m-d"))
{
$row = $result->fetch_array();
}
$currentTaxRate = $row["TaxRate"];

PHP: Commands out of sync; you can't run this command now [duplicate]

This question already has answers here:
Commands out of sync; you can't run this command now
(23 answers)
Closed 4 years ago.
I normally read the data from the database, but I get an error when using the while.
My code is:
$BarberId = 1;
$stmt = $db->prepare("CALL `GetBranch`(?);");
$stmt->bind_param('i', $BarberId);
$stmt->execute();
$Tree_Barber_Id = NULL;
$stmt->bind_result($Tree_Barber_Id);
$stmt->store_result();
if($stmt->num_rows)
{
while($stmt->fetch())
{
$Priod = NULL;
$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");
$stmt2->bind_param('i', $Tree_Barber_Id); //ERROR IS HERE!!!
$stmt2->execute();
$stmt2->bind_result($Priod);
$stmt2->store_result();
}
}
$stmt->close();
I think the error was caused because the variable stmt has not been closed yet. But by closing that by $stmt->close(); while command will not work.
Error is :
Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17
Read the error message.
Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17
Line 17 is this:
$stmt2->bind_param('i', $Tree_Barber_Id);
The error message is telling you $stmt2 is a boolean. $stmt2 comes from this line:
$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");
mysqli's prepare() function returns false if there was an error.
This means your query is invalid. You can find out what's invalid about it by looking at $db->error

PHP Fatal error: Call to undefined method mysqli_stmt::get_result() on Godaddy host [duplicate]

This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Closed 5 years ago.
I am writing some code to fetch data using prepared statements. Below is my code
$statement = $connection->prepare("select * from products where id =?");
$statement->bind_param('d',$id);
$statement->execute();
$result = $statement->get_result();
$product_details = $result->fetch_all();
I am using PHP 5.6.This works fine on my localhost with no error. When I uploaded this file to my Godaddy Host this will gives error:
PHP Fatal error: Call to undefined method mysqli_stmt::get_result()
I searched over internet, but not found satisfied results, On stackoverflow I found several question similar to this but not got solution for me.
On Godddy host I enabled mysqld extension and also running PHP 5.6 So any help
There might be issue with your $statement->execute(). try to run this code in try catch block, may be you find some exception. or run this method with if block.
if($statement){
$result = $statement->get_result();
}
or
try{
$statement = $connection->prepare("select * from products where id =?");
$statement->bind_param('d',$id);
$statement->execute();
if($statement){
$result = $statement->get_result();
}
$product_details = $result->fetch_all();
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

OOP alternative to the mysqli_stmt::fetch_array() [duplicate]

This question already has answers here:
Call to undefined method mysqli_result::fetch()
(2 answers)
Closed 1 year ago.
in this code i get the error: Fatal error: Call to undefined method mysqli_stmt::fetch_array()
What is the problem?
$search= "player";
($sql = $db->prepare('select job from jobs where job like ?'));
$sql->bind_param('s', $search);
$sql->execute();
$sql->bind_result($search);
$data = array();
while ($sql->fetch_array(MYSQLI_ASSOC)) {
$data[] = array(
'label' => $row['job']
);
echo json_encode($data);
}
$sql -> close();
$db -> close();
thanks
Using prepared statements there is no fetch_array(). Use mysqli_stmt::fetch() instead or to fetch multiple records use mysqli_result::fetch_all()
Check the manual: mysqli_stmt::fetch() or mysqli_result::fetch_all()

Error: Call to undefined method mysqli_stmt::fetch_object() [duplicate]

This question already has answers here:
Call to undefined method mysqli_result::fetch()
(2 answers)
Closed 1 year ago.
in this code i get the error: Fatal error: Call to undefined method mysqli_stmt::fetch_array()
What is the problem?
$search= "player";
($sql = $db->prepare('select job from jobs where job like ?'));
$sql->bind_param('s', $search);
$sql->execute();
$sql->bind_result($search);
$data = array();
while ($sql->fetch_array(MYSQLI_ASSOC)) {
$data[] = array(
'label' => $row['job']
);
echo json_encode($data);
}
$sql -> close();
$db -> close();
thanks
Using prepared statements there is no fetch_array(). Use mysqli_stmt::fetch() instead or to fetch multiple records use mysqli_result::fetch_all()
Check the manual: mysqli_stmt::fetch() or mysqli_result::fetch_all()

Categories