# Node red value to mysql

**URL:** <https://discourse.nodered.org/t/node-red-value-to-mysql/26904>\
**Category:** General\
**Created:** [19 May 2020 09:40 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904 "2020-05-19T09:40:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarawut](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sarawut](https://discourse.nodered.org/u/sarawut)\
**Post date:** [19 May 2020 09:40 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/1 "2020-05-19T09:40:09Z")

</div>

Hi all,

Now i tried to send data from node red to mysql but if i have single value it is ok but if more than 2 values how i wirte script in function node

Here is my current flow

Speed1 = msg.payload.Value\_Speed\_1\_SB5\_Recir;  
Speed2 = msg.payload.Value\_Speed\_2\_SB5\_Recir  
PV = msg.payload.Value\_PV\_SB5\_Recir;  
msg.topic = "INSERT INTO mysql ('value\_SB5\_Recir\_Speed1`, 'value\_SB5\_Recir\_Speed2', 'value\_SB5\_Recir\_PV') VALUES ('"+Speed1+"', '"+Speed2+"', '"+PV+"');"  
return msg;

```auto
[{"id":"44997861.54cde8","type":"function","z":"72a03a1a.19aa14","name":"Function send to mysql","func":"Speed1 = msg.payload.Value_Speed_1_SB5_Recir;\n////Speed2 = msg.payload.Value_Speed_2_SB5_Recir\n//PV = msg.payload.Value_PV_SB5_Recir;\nmsg.topic = \"INSERT INTO mysql (`value_SB5_Recir_Speed1`) VALUES ('\"+msg.payload+\"');\"\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":60,"wires":[["22d409d2.9973d6"]]}]

```

Please help me for send data to table at mysql

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 May 2020 10:07 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/2 "2020-05-19T10:07:19Z")

</div>

Hi, first, please edit your post and surround the code and the flow with three backticks

`````  
`code here`  
`````

`````  
`flow here`  
`````

Secondly - you have only sent a function node (not a flow) - so it is quite difficult to help you without understanding where the data comes from. i have my suspicion that the values come from separate nodes & you'll need a join - but I am only speculating due to lack of info.

a screenshot of your flow & a description of its purpose would also help others.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 May 2020 10:59 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/3 "2020-05-19T10:59:06Z")

</div>

I find it much easier to use the `template` node when creating an sql statement if the data is all in the msg object. Here is an example

 ![Screen Shot 2020-05-19 at 6.57.04 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c6abe926af753a09aeb5a316dd71184e961d25ef.png)

note for `text` you need the double quotes around the item.

---

<div class="post-metadata">

**Author:** ![sarawut](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sarawut](https://discourse.nodered.org/u/sarawut)\
**Post date:** [20 May 2020 05:07 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/4 "2020-05-20T05:07:51Z")

</div>

The values come from PLC siemens S7-300

Flow as below  
[{"id":"44997861.54cde8","type":"function","z":"72a03a1a.19aa14","name":"Function send to mysql","func":"Speed1 = msg.payload.Recir\_SB5\_speed1;\nSpeed2 = msg.payload.Recir\_SB5\_speed2;\nPV = msg.payload.Recir\_SB5\_PV;\nmsg.topic = "INSERT INTO mysql (value\_SB5\_Recir\_Speed1, value\_SB5\_Recir\_Speed2, value\_SB5\_Recir\_PV) VALUES('"+msg.payload.Recir\_SB5\_speed1 +"', '"+msg.payload.Recir\_SB5\_speed2 +"', '"+msg.payload +"');"\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":180,"wires":[["22d409d2.9973d6","87ed3551.a65c68"]]}]

![e](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7dedc9313630877ca421077552ddfc15e9266220.jpeg) ![q](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6edc187d6b86c0855968f10959467fb7c2a5b343.jpeg) ![w](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/9027dd242bfcf84ae40acc56e34fcc8d78b2e265.jpeg)

---

<div class="post-metadata">

**Author:** ![sarawut](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sarawut](https://discourse.nodered.org/u/sarawut)\
**Post date:** [20 May 2020 05:14 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/5 "2020-05-20T05:14:10Z")

</div>

If i use template node no need function node right ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [20 May 2020 06:52 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/6 "2020-05-20T06:52:34Z")

</div>

Excellent info.

Firstly though, in order to make code more readable and importable it is important to post it between two sets of three backticks - ``` - see this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Secondly, you don't have debug before the function. It is impossible to see what is going on to your function.

And again, your flow export is only the function. When exporting, select all nodes you wish to export before opening the export dialog.

Lastly, my suspicion was correct...

> [@Steve-Mcl](#):
>
> i have my suspicion that the values come from separate nodes & you'll need a join

Watch [this 4m video](https://youtu.be/t8T-JMnQHMk) to understand what I mean.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 May 2020 08:40 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/7 "2020-05-20T08:40:17Z")

</div>

> [@sarawut](#):
>
> If i use template node no need function node right ?

Correct, BUT as @Steve-Mcl said, you do need to add a `join` since you have three seperate messages.

One last thing, when uploading images please make them bigger. Your images are so small that I can not read any of the text.

---

<div class="post-metadata">

**Author:** ![sarawut](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@sarawut](https://discourse.nodered.org/u/sarawut)\
**Post date:** [20 May 2020 09:48 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/8 "2020-05-20T09:48:09Z")

</div>

thank now done already i use global.get for this case

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 May 2020 10:39 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/9 "2020-05-20T10:39:39Z")

</div>

> [@sarawut](#):
>
> i use global.get for this case

?? How?? How are you making sure that you get the data from all three inputs? What are the input nodes anyway? You never provided your entire flow and the image is too small to make out.

If you have solved yout issue, you should fully explain what you did in case someone else reads this in the future while tring to solve an issue of their own.

---

<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:** [19 July 2020 10:43 UTC](https://discourse.nodered.org/t/node-red-value-to-mysql/26904/10 "2020-07-19T10:43:16Z")

</div>

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