Can JavaScript connect with MySQL? If so, how?
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
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 understood your question I think you are confusing it with languages like dot.net and java where you can open DB connection within your code. No, JavaScript can not directly connect to MySQL as JavaScript is a client side scripting language(Exception Node.js).You need a middle layer like RESTful API to access data.
No.
You need to write a wrapper in PHP, and then export the returned data (probably as Json). NEVER, get from your "_GET" the SQL code, as this is called an SQL injection (people who learn this will have full control over your database).
This is an example I wrote:
function getJsonData()
{
global $db;
if (!$db->isConnected()) {
return "Not connected";
}
$db->query("SELECT * FROM entries");
$values = array();
while( $v = $db->fetchAssoc()){
$values[] = $v;
}
return json_encode($values);
}
switch (@$_GET["cmd"]){
case 'data':
print getJsonData();
exit;
default:
print getMainScreen();
exit;
}
Do learn about SQL injections please.
YES? Have a look a meteor. Links:
http://meteor.com/screencast and http://net.tutsplus.com/tutorials/javascript-ajax/whats-this-meteor-thing/
I don't understand how it is done. But Nettuts+ put this thing in the javascript-ajax section, maybe magic happens.
It also shows some way to connect and insert to MongoDB with JS, like this:
Products.insert({Name : "Hammer", Price : 4.50, InStock : true});
Products.insert({Name : "Wrench", Price : 2.70, InStock : true});
Products.insert({Name : "Screw Driver", Price : 3.00, InStock : false});
Products.insert({Name : "Drill", Price : 5.25, InStock : true});
Simple answer is: no.
JavaScript is a client-side language that runs in the browser (node.js notwithstanding) and MySQL is a server-side technology that runs on the server.
That means you typically use a server-side language like ASP.NET or PHP to connect to the database.