NodeJS + PHP

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

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

    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>```

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?

Inside includes/conJava.php is the following code.

<?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.

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.

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?

sure, dopey question from me :slight_smile: Maybe this link could help: Where Does Node.js And Require() Look For Modules?. And as questioned: ['mysql'] is an array, shouldn't it be 'mysql'?

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

screen2

seems a bit strange to me: Modules: CommonJS modules | Node.js v19.7.0 Documentation

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