Simple Check for Select Query Empty Result

Simple check for SELECT query empty result


SELECT * FROM service s WHERE s.service_id = ?;

-- do stuff here.....

According to SQL Server Books Online:

Returns the number of rows affected by
the last statement. If the number of
rows is more than 2 billion, use

SQL return result on empty query

SELECT '' AS age
SELECT Name FROM Contact WHERE Age > @age

This will check if a result set will return and if nothing will, select a blank space and name the field age.

SELECT query return error on Empty result

Try replacing

FIND_IN_SET(`Notification`.`id`,(SELECT read_ids FROM read_notifications WHERE user_id = 46))


FIND_IN_SET(`Notification`.`id`,IFNULL((SELECT read_ids FROM read_notifications WHERE user_id = 46), ''))

From the manual, if the subquery result is empty it will return NULL, and FIND_IN_SET will return NULL if either argument is NULL, and NOT NULL is still NULL, as is NULL AND 1, and NULL is equivalent to false. So when the subquery returns NULL, your WHERE condition will fail.

By adding an IFNULL around the subquery result, you can get it to return a value which is valid for FIND_IN_SET, which will allow your query to work as expected.

Check empty result with mysql query

Try on below:

select * from section s where idsection not in (select page.idsection from 
page where
page.idsection=s.idsection and idpage not in (select idpage from read));

I use not in because u need to check all pages instead of 1 of the page.
So we should get the page that are not in read (unread) and put not in there.

If u have 1 section that have 5 pages and all is read, it will give you empty result on subquery making ur main query selecting the session.

If u have 1 section that have 10 pages that only 9 pages is read, it will give u pages no 10 in subquery and make your section in main query empty (not in).

How do I check if a table is empty or already my query didn't match any results

I guess, that $conn is a PDO connection? In that case, the method $conn->query() returns an object of type PDOStatement. See

The method does NOT return the result set.

Instead you can use the PDOStatement object, to fetch the results:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetchAll();

{ ... }

In case you are using mysqli, the object returned by query() is this:

So the code would be:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);

{ ... }

Please also note: Your code is highly insecure! You should use prepared statements to prevent sql-injection:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = :currentUrl";
$stmt = $conn->prepare($sql);
$stmt->execute(['currentUrl' => $currentURL]);
$result = $stmt->fetchAll();

{ ... }

select query in mysql always return empty result

It's because the values are NULL. NULL cannot be captured by an = or != comparison. Use the following instead:

FROM `rdm_order`
WHERE `aff_result` != 'xyc'
OR `aff_result` IS NULL

Related Topics

Leave a reply