Skip to content
PHP Logo

How secure is the msql_connect() command in PHP and what are the potential risks associated with it?

The msql_connect() command in PHP is not secure as it is vulnerable to SQL injection attacks. To mitigate this risk, it is recommended to use prepared statements with parameterized queries instead.

php
// Establishing a secure database connection using PDO with prepared statements
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}