What are the limitations of using JavaScript for server-side operations like database queries?

One limitation of using JavaScript for server-side operations like database queries is that it is not as secure as server-side languages like PHP. To address this limitation, it is recommended to use a server-side language like PHP to handle database queries to prevent exposing sensitive information and vulnerabilities in the code.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform database query
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>