MySQL <= 5.2 - OOP connect to database in class

349 Views Asked by At

I CAN'T USE MYSQLI OR PDO

I'm currently working on a legacy project (5.2) where I can't use fancy extensions like MySQLi or PDO - Only MySQL - How would I connect to a database and use that connection in a class?

Basically this

public function __construct($pdo) {
$this->db = $pdo;

}

As 5.2 MySQL / PHP

I just need a simple example to get started.

Thanks!

1

There are 1 best solutions below

10
On

PDO Connection through PHP I highly recommend to use PDO rather than mysqli.. pdo is faster and use prepared statements to avoid sql injection, and Filter, Sanitize the user inputs.

<?php
session_start();

$servername = "localhost";
$username = "username";
$password = "password";

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

// Check connection
if (mysqli_connect_error()) {
    die("Database connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

You can use the above code for PHP 5.2.9 and 5.3.0