How to check if query returned no rows #3312
Unanswered
DragonOsman
asked this question in
Q&A
Replies: 1 comment
-
Hi, @DragonOsman 🙋🏻♂️ Based on your title, you can just check the length of const isEmpty = rows.length === 0; Now based in your example, a common/classic way is to check if there is a data in the database, is to count the rows directly in the query, for example: SELECT COUNT(*) AS `total` FROM `books` WHERE `title` = ? AND `isbn` = ?; It will return an array like: rows[0].total; // number There is also the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. I want to know how I can check if my query returned no rows.
I'm using MySQL Server version 9.1 with Next.js 15. I'm trying to build a personal library app, intended to be app where users can log in to manage a list of books they've read.
This is my code for an API route to add a book; first it tries to query the database to see if the book is already in there, and if it isn't, it adds it. Otherwise, it redirects to update route. What I know is how I can properly determine if the book is there or not by checking if the query returned any rows or not.
If I'm doing it wrong, what's the correct way? Do I need to write the code to add the book inside the
catch
block and keep theredirect
function where it is? Or what?Beta Was this translation helpful? Give feedback.
All reactions