What are the differences between the mysqli_ and mysql_ functions used in the PHP script for database access?

The main difference between the mysqli_ and mysql_ functions in PHP is that mysqli_ functions are used for interacting with databases using MySQLi extension, which supports improved security features and prepared statements for preventing SQL injection attacks. On the other hand, mysql_ functions are used with the older MySQL extension, which is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Therefore, it is recommended to use mysqli_ functions for database access in PHP scripts.

// Using mysqli_ functions for database access
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Perform SQL 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";
}

// Close connection
$conn->close();