# Database, problem with require

**URL:** <https://discourse.nodered.org/t/database-problem-with-require/28017>\
**Category:** General\
**Created:** [8 June 2020 23:34 UTC](https://discourse.nodered.org/t/database-problem-with-require/28017 "2020-06-08T23:34:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robinhttp](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@robinhttp](https://discourse.nodered.org/u/robinhttp)\
**Post date:** [8 June 2020 23:34 UTC](https://discourse.nodered.org/t/database-problem-with-require/28017/1 "2020-06-08T23:34:09Z")

</div>

hi!  
I should read from a database (I used xaamp with a mysql database). I put this code in Function (node), but I'm having problems with 'require'. I followed the guide to add external modules, but I can't solve it. Could anyone show some examples of database reading?  
FUNCTION:

```auto
var mysql = require('mysql');

var connection = mysql.createConnection( { host : 'localhost',
  user : 'nome_utente',
  password : 'password_utente',
  database : 'nome_db'
} );
connection.connect();

var queryString = 'SELECT cmp FROM tbl ORDER By id DESC';

connection.query(queryString, function(err, rows, fields) {
  if (err) throw err;
  for (var i in rows) {
      console.log('Valori: ', rows[i].cmp);
  }
});

connection.end();

```

---

<div class="post-metadata">

**Author:** ![thebaldgeek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thebaldgeek/32/93523_2.png) [@thebaldgeek](https://discourse.nodered.org/u/thebaldgeek)\
**Post date:** [9 June 2020 01:18 UTC](https://discourse.nodered.org/t/database-problem-with-require/28017/2 "2020-06-09T01:18:00Z")

</div>

I use MySQL. I use the MySQL node. [https://flows.nodered.org/node/node-red-node-mysql](https://flows.nodered.org/node/node-red-node-mysql)  
It is very easy. No 'require' at all.  
msg.topic has the select statement and it just works.

---

<div class="post-metadata">

**Author:** ![robinhttp](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@robinhttp](https://discourse.nodered.org/u/robinhttp)\
**Post date:** [9 June 2020 07:24 UTC](https://discourse.nodered.org/t/database-problem-with-require/28017/3 "2020-06-09T07:24:47Z")

</div>

Thanks, your solution is certainly more efficient!

---

<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:** [23 June 2020 07:24 UTC](https://discourse.nodered.org/t/database-problem-with-require/28017/4 "2020-06-23T07:24:48Z")

</div>

Closed on 23 June 2020 07:24 UTC

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