I want to check whether my prepared query has returned empty or not without having to go into a loop. This is the code I have tried using:
if(empty($pQuery1->fetch(PDO::FETCH_ASSOC))){}
When I try this I get the error:
Fatal error: Can't use method return value in write context
Whether I use PDO->fetchALL
or PDO->fetch
I receive the same error. Should I be doing something differently?
The fetch_all() / mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both. Note: This function is available only with MySQL Native Driver.
PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch.
The PHP Data Objects (PDO) defines a lightweight interface for accessing databases in PHP. It provides a data-access abstraction layer for working with databases in PHP. It defines consistent API for working with various database systems.
You need to assign the results to a variable, then call empty()
on the variable. It's just an annoying limitation of the empty()
function. See this question.
$results = $pQuery1->fetch(PDO::FETCH_ASSOC);
if (empty($results)){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With