Can JavaScript connect with MySQL?

2019-01-01 00:58发布

Can JavaScript connect with MySQL? If so, how?

18条回答
情到深处是孤独
2楼-- · 2019-01-01 01:01

JavaScript can't connect directly to DB to get needed data but you can use AJAX. To make easy AJAX request to server you can use jQuery JS framework http://jquery.com. Here is a small example

JS:

jQuery.ajax({
type: "GET",
dataType: "json",
url: '/ajax/usergroups/filters.php',
data: "controller=" + controller + "&view=" + view,
success: function(json)
{
    alert(json.first);
    alert(json.second);
});

PHP:

$out = array(); 
$out['first']   = 'first value';
$out['second']   = 'second value';
echo json_encode($out);
查看更多
像晚风撩人
3楼-- · 2019-01-01 01:02

Bit late but recently I have found out that MySql 5.7 got http plugin throuh which user can directly connect to mysql now.

Look for Http Client for mysql 5.7

查看更多
浮光初槿花落
4楼-- · 2019-01-01 01:05

Client-side JavaScript cannot access MySQL without some kind of bridge. But the above bold statements that JavaScript is just a client-side language are incorrect -- JavaScript can run client-side and server-side, as with Node.js.

Node.js can access MySQL through something like https://github.com/sidorares/node-mysql2

You might also develop something using Socket.IO

Did you mean to ask whether a client-side JS app can access MySQL? I am not sure if such libraries exist, but they are possible.

EDIT: Since writing, we now have MySQL Cluster:

The MySQL Cluster JavaScript Driver for Node.js is just what it sounds like it is – it’s a connector that can be called directly from your JavaScript code to read and write your data. As it accesses the data nodes directly, there is no extra latency from passing through a MySQL Server and need to convert from JavaScript code//objects into SQL operations. If for some reason, you’d prefer it to pass through a MySQL Server (for example if you’re storing tables in InnoDB) then that can be configured.

查看更多
素衣白纱
5楼-- · 2019-01-01 01:05

Depending on your environment, you could use Rhino to do this, see Rhino website. This gives you access to all of the Java libraries from within JavaScript.

查看更多
萌妹纸的霸气范
6楼-- · 2019-01-01 01:06

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

查看更多
伤终究还是伤i
7楼-- · 2019-01-01 01:06

You can add mysql connection using PHP file. Below is the example of PHP file.

<?php
   $con = mysql_connect('localhost:3306', 'dbusername', 'dbpsw');
   mysql_select_db("(dbname)", $con);

   $sql="SELECT * FROM table_name";

   $result = mysql_query($sql);

   echo " <table border='1'>
   <tr>
   <th>Header of Table name</th>
   </tr>";

   while($row = mysql_fetch_array($result))
   {
     echo "<tr>";
     echo "<td>" . $row['(database_column_name)'] . "</td>";
     echo "<td>" . $row['database_column_name'] . "</td>";
     echo "</tr>";
    }
    echo "</table>";
    mysql_close($con);
   ?> }
查看更多
登录 后发表回答