-4

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!

Dandersen
  • 19
  • 5

1 Answers1

0

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

Ajmal PraveeN
  • 414
  • 8
  • 16
  • MySQLi also allows prepared statements – Mark Baker Sep 18 '17 at 08:57
  • @MarkBaker for Query optimization for each activity PDO is the Best comparing the mysqli.. my own experience. – Ajmal PraveeN Sep 18 '17 at 08:59
  • Can't use MySQLi either. – Dandersen Sep 18 '17 at 08:59
  • 1
    @AjmalPraveen - That is very subjective, and arguably not the case... but your answer seems to suggest that PDO is better than MySQLi because it allows prepared statements, when this isn't the case because both allow prepared statements – Mark Baker Sep 18 '17 at 09:00
  • @MarkBaker I didn't mentioned PDO is better because of Prepared Statements. I recommended that for Speed and Self Optimized comparing mysqli.. advice from my own experience. and i mentioned use prepared statements didn't mentioned that is pdo or mysqli. i know that prepared statements can be used mysqli and pdo – Ajmal PraveeN Sep 18 '17 at 09:02
  • @AjmalPraveen - I CAN'T use MySQLi – Dandersen Sep 18 '17 at 09:02
  • @Dandersen Lol so what you need? mysql ? depreciated one? – Ajmal PraveeN Sep 18 '17 at 09:03
  • @AjmalPraveen `I highly recommend to use PDO rather than mysqli.. pdo is faster and use prepared statements to avoid sql injection`.... seems to suggest that MySQLi can't use prepared statements – Mark Baker Sep 18 '17 at 09:04
  • @AjmalPraveen Yes! Have you even read my question? It's a legacy project where I can't use either MySQLi or PDO. – Dandersen Sep 18 '17 at 09:04
  • @MarkBaker There can be mistakes because english is not my native lang.. So usually i re write the user codes.. Its better than the advice. – Ajmal PraveeN Sep 18 '17 at 09:06