# NodeJS + PHP

**URL:** <https://community.unix.com/t/nodejs-php/390378>\
**Category:** Web Development\
**Created:** [March 14, 2023, 12:52pm UTC](https://community.unix.com/t/nodejs-php/390378 "2023-03-14T12:52:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remo](https://community.unix.com/letter_avatar/remo/32/6_22734026db12803ebb3e059622ac3534.png) [@Remo](https://community.unix.com/u/Remo)\
**Post date:** [March 14, 2023, 12:52pm UTC](https://community.unix.com/t/nodejs-php/390378/1 "2023-03-14T12:52:39Z")

</div>

Hello,

I'm trying to use NodeJS with PHP.  
I want to use NodeJS to instantly insert information into a MYSQL database without refreshing the page. (With PHP you have to refresh).

Only thing is. I installed NodeJS and RequireJS. But it cant find the mysql.js file which is included with NodeJS. Do you have to set up the path towards it. Or instal it in the same directory in PHP. I dont know whats going on.

I also added a screenshot with the error code i recieve from the console.

![screen](https://community.unix.com/uploads/default/original/3X/9/0/903f8abddf203c91638ba29c5f9a134369989ed7.png)

The code im using to insert the information towards the database with Javascript.

````plaintext
    document.getElementById('addDatabase').onclick = function() {

        var dateNow = "<?php echo $date; ?>";
        var orderlog = "<?php echo $idorderlogFinal; ?>";
        var idorder = "<?php echo $row->idorder; ?>";
        var username = "<?php echo $_SESSION["username"]; ?>";
        var inputVal = document.getElementById("textDatabase").value;

        var mysql = require(['mysql']);

        <?php
            include('includes/conJava.php')
        ?>

        con.connect(function(err) {
           if (err) throw err;
           console.log("Connected!");
           var sql = "INSERT INTO picqer_salesorder_notes (idorder_log, idorder, `user`, description, created_at) VALUES (orderlog, idorder, username, inputVal, dateNow)";
           con.query(sql, function (err, result) {
               if (err) throw err;
               console.log("1 record inserted");
           });
        });
  }
</script>```
````

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [March 14, 2023, 2:26pm UTC](https://community.unix.com/t/nodejs-php/390378/2 "2023-03-14T14:26:15Z")

</div>

Hi @Remo,

where do you initialise the connection? You define a `mysql` object, but then you call `con.connection`. `con` is a JS object, too, where does it come from? And what is `includes/conJava.php` for?

Furthermore, `['mysql']` is an array, is that really correct?

---

<div class="post-metadata">

**Author:** ![Remo](https://community.unix.com/letter_avatar/remo/32/6_22734026db12803ebb3e059622ac3534.png) [@Remo](https://community.unix.com/u/Remo)\
**Post date:** [March 14, 2023, 2:52pm UTC](https://community.unix.com/t/nodejs-php/390378/3 "2023-03-14T14:52:49Z")

</div>

Inside includes/conJava.php is the following code.

```php
<?php
    echo 'var con = mysql.createConnection({
        host: "",
        user: "",
        password: "",
        database: ""
    });'
?> 

```

I changed the values to empty since its important information.

I defined the MYSQL object because its the object NodeJS is using to acces the database. This should be inside the npm instal package of NodeJS.

Also good to notice, Im doing this on a Debian Server.

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [March 14, 2023, 3:05pm UTC](https://community.unix.com/t/nodejs-php/390378/4 "2023-03-14T15:05:29Z")

</div>

> [@Remo](#):
>
> I defined the MYSQL object because its the object NodeJS is using to acces the database

well of course, it could be quite difficult to access a db without a connector... But why do you define the connector `con` (= connection object) in an extra PHP script instead of directly in the JS code? Btw, `mysql = ...` gets the `mysql` **module** , not the connector. Using that module, you can instantiate a connector.

---

<div class="post-metadata">

**Author:** ![Remo](https://community.unix.com/letter_avatar/remo/32/6_22734026db12803ebb3e059622ac3534.png) [@Remo](https://community.unix.com/u/Remo)\
**Post date:** [March 14, 2023, 3:13pm UTC](https://community.unix.com/t/nodejs-php/390378/5 "2023-03-14T15:13:17Z")

</div>

I included the con with a PHP script because i dont want important connection information inside the index page.

Mysql = gets indeed the module. But also that module he can't find. Do you have to install the NodeJS package in the JS folder of your project where your working in?

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [March 14, 2023, 3:24pm UTC](https://community.unix.com/t/nodejs-php/390378/6 "2023-03-14T15:24:14Z")

</div>

> [@Remo](#):
>
> I included the con with a PHP script because i dont want important connection information inside the index page.

sure, dopey question from me 🙂 Maybe this link could help: [Where Does Node.js And Require() Look For Modules?](https://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm). And as questioned: `['mysql']` is an array, shouldn't it be `'mysql'`?

---

<div class="post-metadata">

**Author:** ![Remo](https://community.unix.com/letter_avatar/remo/32/6_22734026db12803ebb3e059622ac3534.png) [@Remo](https://community.unix.com/u/Remo)\
**Post date:** [March 14, 2023, 3:51pm UTC](https://community.unix.com/t/nodejs-php/390378/7 "2023-03-14T15:51:33Z")

</div>

The reason i did the brackets arround 'mysql' is because the error message i recieved recommended me to do it.

![screen2](https://community.unix.com/uploads/default/original/3X/7/5/7526c2f94c562d9f1db41a70567d7bb2a396e05d.png)

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [March 14, 2023, 4:02pm UTC](https://community.unix.com/t/nodejs-php/390378/8 "2023-03-14T16:02:29Z")

</div>

seems a bit strange to me: [Modules: CommonJS modules | Node.js v19.7.0 Documentation](https://nodejs.org/api/modules.html#modulerequireid)

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [March 21, 2023, 4:03pm UTC](https://community.unix.com/t/nodejs-php/390378/9 "2023-03-21T16:03:25Z")

</div>

Closed on March 21, 2023, 4:03pm UTC

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