International PHP Conference Munich 2026

Original MySQL API

Introduction

This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

add a note

User Contributed Notes 1 note

up
0
taegmyial at gmail dot com
1 month ago
<?php
// 1. Session start must always be at the very top
session_start();

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_db";

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

// If the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $user = $_POST['username'];
    $pass = $_POST['password'];

    // 2. Find the user in the database by username
    // (Prepared Statements should be used here for security, see Topic 24)
    $sql = "SELECT id, password_hash FROM users WHERE username = '$user'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();

        // 3. Verify the password using password_verify
        if (password_verify($pass, $row['password_hash'])) {

            // Password is correct -> Save user data into Session
            $_SESSION["loggedin"] = true;
            $_SESSION["userid"] = $row["id"];
            $_SESSION["username"] = $user;

            echo "Welcome, you are logged in!";
        } else {
            echo "Incorrect password.";
        }
    } else {
        echo "User does not exist.";
    }
}
?>
To Top