Checking for an empty result (PHP, PDO, and MySQL)
You're throwing away a result row when you do $sth->fetchColumn()
. That's not how you check if there are any results. You do
if ($sth->rowCount() > 0) {
... got results ...
} else {
echo 'nothing';
}
Relevant documentation is here: PDOStatement::rowCount
How to check fetched result set is empty or not?
Check $data
variable like:
if ($data) {
//not empty
} else {
// empty
}
If the result of SELECT
query did return any data, variable $data
will contain a non-empty array/object which evaluates to true
, and a false-like value otherwise.
php cannot check if a PDO result is empty using empty() returns FATAL ERROR
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)){}
How can I check if a row is empty in mySQL using PDO?
Both other answers are essentially unacceptable.
And not because they lack cleanness but because they are awfully dangerous
public function Login ($email,$password)
{
$sql="SELECT 1 FROM db_user WHERE email=? AND password=?";
$stmt = $this->_dbHandle->prepare($sql);
$statement -> execute([$email, $password]);
return $stmt->fetchColumn();
}
You should be using prepared statements, not just mimicking them.
PDO results are empty
You are assigning values to $bind_array
and $bnid_array
but are sending in $bind_arguments
to execute()
. Try changing $bnid_array
to $bind_array
and use $stmt->execute($bind_array);
Check for PDO Results, If None Display Message, If Yes then Loop Through
You need not fetch()
but fetchAll()
.
PHP PDO Check if database query returns NULL values
If I am not mistaken, you are using "$result" where you should be using "$result_table" - check the comment below:
$query = $this->dbh->prepare($sql);
$query->bindParam(':phone', $phone);
$query->bindParam(':name', $name);
$result = $query->execute();
if ($result) {
if ($isVitamin) {
$result_table = $query->fetch(PDO::FETCH_ASSOC);
$result_table['isVitamin'] = 1;
if (empty($result)) {
return false;
} else {
return $result_table;
}
} else {
$result_table = $query->fetch(PDO::FETCH_ASSOC);
if (empty($result)) { //<-- this should be $result_table ?
return false;
} else {
// echo "inside else";
// echo $result_table['name'];
$result_table['isVitamin'] = 0;
return $result_table;
}
}
} else {
return false;
}
Related Topics
How to Detect If Have to Apply Utf-8 Decode or Encode on a String
How to Check If Directory Contents Has Changed with PHP
What Security Problems Could Come from Exposing PHPinfo() to End Users
Basic Simplexml Working Example
How to Send a Bytearray (From Flash) and Some Form Data to PHP
Make Multiple Files to Force-Download
Uncompress a Gzip File from Curl, on PHP
PHP & MySQL: When Exactly to Use HTMLentities
PHP Session Ids -- How Are They Generated
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"
JSON Parse Error with Double Quotes
PHP Type-Hinting to Primitive Values