JS
(quick guide & examples)
CLIENT-SIDE JAVASCRIPT Run Javascript on a webpage.
Usual practice for client-side - JS calls a server-side script, server-side script then connects to the database.
This is for security purposes, we do not directly expose a database to the Internet.
JAVASCRIPT CALL PHP SCRIPT fetch("DB.PHP") .then(res => res.text()) .then(txt => console.log(txt));
PHP CONNECT TO DATABASE $pdo = new PDO("mysql:host=HOST; dbname=NAME;charset=utf8mb4", USER, PASSWORD);
FETCH DATA & OUTPUT $stmt = $pdo->prepare("SELECT * FROM `TABLE`"); $stmt->execute(); print_r($stmt->fetchAll());
CONNECT TO DATABASE const mysql = require("mysql"); const db = mysql.createConnection({ host: HOST, user: USER, password: PASS, database: DB }); db.connect(err => { if (err) { throw err; } });
FETCH DATA db.query("SELECT * FROM `TABLE`", (err, res) => console.log(res)});