Can JavaScript Connect with MySQL

Can JavaScript connect with MySQL?

No, JavaScript can not directly connect to MySQL. But you can mix JS with PHP to do so.

JavaScript is a client-side language and your MySQL database is going to be running on a server

How to connect to MySQL from JavaScript?

A backend repeater is always needed. For this issue you can set up a light-weight server that forwards your database accessing request to mysql server using, say node.js.

How do I connect javascript and MySQL via PHP?

Try using the <?php include 'filepath' ; ?> includes reading documentation here, I think that is what you need

<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>

<script>
<?php include 'getValueFromDB.php' ; ?>
var value = "<?php echo $someVar; ?>";
if(value == 0){
document.getElementsByName("button1")[0].disabled = true;
}
</script>

You can not tell javascript how to use PHP, because JS is a client language and a PHP server language and the workflow is first PHP and second JS and not vice versa.

If you need to take php data with JS, you need to use AJAX

well (it's an example, not tested)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="someOtherScript.php" method="get">
<input type="submit" name="button1" value="Run me now!">
</form>

<script>
$.ajax({
url: "getValueFromDB.php",
success: function(result){
if(result == 0){
document.getElementsByName("button1")[0].disabled = true;
}
}});
</script>

php

 <?php
<!-- retrieve value from DB here (This part is not the problem)-->
$someVar = 0;
echo $someVar
?>

Connecting to a MySQL Database and do a query using only client side Javascript and HTA

A HUGE thanks to Kul-Tigin for providing the answer for the USE of ADO ActiveX which I did not even think about. I was not searching properly the ODBC connection methods and always fell on VBScript. So here is a working code of a personal test I did after installing the latest MySQL ODBC Connector as of the date of this comment.

var hmess = document.getElementById("mess");
var oconn = new ActiveXObject("ADODB.Connection");
var ors = new ActiveXObject("ADODB.Recordset");
var sconn = "";
var scn_driver = "DRIVER=MySQL ODBC 8.0 Unicode Driver;";
var scn_server = "SERVER=localhost;";
var scn_database = "DATABASE=DatabaseName;";
var scn_userid = "USER ID=UserName;";
var scn_password = "PASSWORD=UserPassword;";
var ssql = "SELECT * FROM Table WHERE IDField=1";

sconn = scn_driver + scn_server + scn_database + scn_userid + scn_password;

oconn.Open(sconn);
ors.Open(ssql,oconn);
ors.MoveFirst();
hmess.innerHTML = ors("TableFieldName");
ors.Close();
oconn.Close();

Thank you for the answers and your help.

how to connect Website to MYSQL database?

ideally you should be writing some server side code to add this sort of information to a database. that way you can secure access to authorised users (i.e. logged in users) and your queries cannot be modified on the browser by any user. you can use a programming language like PHP (with a framework like Yii, CodeIgniter) which is quite lightweight.

Is it possible to access a MySQL database straight from Javascript

Javascript, if run in the browser, has no way of accessing a MySQL Database. For one, this is a technical limitation, because Javascript has no way of communicating arbitrary protocols (no, WebSockets are not the solution). Note that Node.js, being server side and all, is a "different kind of javascript".

Then there is the security issue. If your javascript could access the database directly, I could easily access the database myself. I would be able to read and manipulate the same data your javascript can. Security-wise calling this a nightmare would be an euphemism.

You'll have to - and WANT TO - route database access through a server side application. If that app is written in PHP, C# or Assembly doesn't really matter much. With Node.js you can even use Javascript on the server side. Use what you're comfortable with.



Related Topics



Leave a reply



Submit