# Check the Result of a MySQL Query

**URL:** <https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621>\
**Category:** General\
**Tags:** database\
**Created:** [23 December 2021 16:59 UTC](https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621 "2021-12-23T16:59:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_R\_A\_L\_F](https://avatars.discourse-cdn.com/v4/letter/_/9fc348/32.png) [@\_R\_A\_L\_F](https://discourse.nodered.org/u/_R_A_L_F)\
**Post date:** [23 December 2021 16:59 UTC](https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621/1 "2021-12-23T16:59:43Z")

</div>

Hello,  
as you can see on the picture, make a MySQL query to it can come to the following states:

1. query to DB =\> the "chipnr" is not present, then I get [empty].
2. query to DB =\> the "chipnr" is available and I get the value in "payload[0].chipnr".
3. output from DB =\> The entry was successful and I get msg.payload: OkPacket "[object Object]"

How do I have to set the switch node to get the output like this:

1. state =\> value 1
2. state =\> value 2
3. state =\> value 3

Thanks

 ![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f1b1475f46ddd97ac4cca23b8c7aea871f952b7.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 December 2021 17:39 UTC](https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621/2 "2021-12-23T17:39:34Z")

</div>

I'm not sure if this is what you are asking, but you could change your SELECT query to  
select count(\*) as recordcount from benutzer\_hi\_ma where chipnr = 20000123  
Then the first two cases have msg.payload[0].recordcount == 0 or 1

I think that for the INSERT, msg.payload.affectedRows == 1 to indicate success, or else an error not a msg.payload.

---

<div class="post-metadata">

**Author:** ![\_R\_A\_L\_F](https://avatars.discourse-cdn.com/v4/letter/_/9fc348/32.png) [@\_R\_A\_L\_F](https://discourse.nodered.org/u/_R_A_L_F)\
**Post date:** [24 December 2021 11:00 UTC](https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621/3 "2021-12-24T11:00:09Z")

</div>

@jbudd Thanks for the tip.

I have now solved it as follows:  
I swapped the SQL query with the select count(\*).  
Then I replaced the switch with a function node and put a "Convert to JavaScript-Object" in front of the function and built the function like this:

```auto
> // define topic
> msg.topic = "RMChipScan";
> 
> // if insert success
> if (msg.payload.affectedRows == 1) {
> msg.payload = "entered";
> }
> // if chipnr isnt available
> else if (msg.payload[0]["count(chipnr)"] == 0) {
> msg.payload = "does not match";
> }
> // if chipnr is available
> else if (msg.payload[0]["count(chipnr)"] == 1) {
> msg.payload = "matches";
> }
> 
> return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 January 2022 11:00 UTC](https://discourse.nodered.org/t/check-the-result-of-a-mysql-query/55621/4 "2022-01-07T11:00:25Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
