Gmail / Facebook Style Chat Application
Details Live Demo Download


Posted: Thursday 9th of September 2021

How to change or convert MySQL to MySQLi



In PHP 7 or greater, the MySQL extension has been removed completely and so, it is important to convert or upgrade from MySQL to MySQLi to keep your website updated
One of the most important developments in the PHP world was the backward compatibility break for the PHP MySQL extension, which leaves us with two methods to connect to the database: MySQLi and PDO

I will discuss how to convert a MySQL extension into MySQLi. The first thing you should understand is that MySQL works as a resource whereas MySQLi works as a resource and an object. While you really do not need to know the technical differences, you must understand that these two are a lot different from each other.

The functions defined below uses the MySQLi Procedural and Object Oriented methods to convert all deprecated MySQL functions to MySQLi

To use these functions, simply copy all the codes below to your database connection file, update your database connection details and you are good to start using them

Code:

<?php
define ('hostnameorservername','localhost'); //Your server name or hostname goes in here
define ('serverusername','root'); //Your database username goes in here
define ('serverpassword',''/); //Your database password goes in here
define ('databasenamed','scmplus'); //Your database name goes in here

$mysqli = new mysqli( hostnameorservername, serverusername, serverpassword, databasenamed );

if ($mysqli->connect_errno)
{
printf("Connection failed: %s\n", $mysqli->connect_error);
exit();
}

if (!$mysqli->set_charset("utf8"))
{
printf("Error loading character set utf8: %s\n", $mysqli->error);
exit();
}

if ( !function_exists('mysql_connect') )
{
function mysql_connect( $sql_host, $sql_username, $sql_password )
{
$mysqli_connect = mysqli_connect( $sql_host, $sql_username, $sql_password );
return $mysqli_connect;
}
}

if ( !function_exists('mysql_select_db') )
{
function mysql_select_db( $database, $connection )
{
$mysqli_select_db = mysqli_select_db( $connection, $database );
return $mysqli_select_db;
}
}

if (!function_exists('mysql_real_escape_string'))
{
function mysql_real_escape_string($string)
{
global $mysqli;
if($string){
$real_escape_string = $mysqli->real_escape_string($string);
return $real_escape_string;
}
}
}

if (!function_exists('mysql_query'))
{
function mysql_query($query)
{
global $mysqli;
if($query) {
$result = $mysqli->query($query);
return $result;
}
}
}

if (!function_exists('mysql_fetch_array'))
{
function mysql_fetch_array($result){
if($result){
$row = $result->fetch_assoc();
return $row;
}
}
}

if (!function_exists('mysql_num_rows'))
{
function mysql_num_rows($result){
if($result){
$row_cnt = $result->num_rows;;
return $row_cnt;
}
}
}

if (!function_exists('mysql_free_result'))
{
function mysql_free_result($result)
{
if($result){
global $mysqli;
$result->free();
}
}
}

if (!function_exists('mysql_data_seek'))
{
function mysql_data_seek($result, $offset){
if($result){
global $mysqli;
return $result->data_seek($offset);
}
}
}

if (!function_exists('mysql_close'))
{
function mysql_close(){
global $mysqli;
return $mysqli->close();
}
}
if (!function_exists('mysql_insert_id'))
{
function mysql_insert_id(){
global $mysqli;
$lastInsertId = $mysqli->insert_id;
return $lastInsertId;
}
}

if (!function_exists('mysql_error'))
{
function mysql_error(){
global $mysqli;
$error = $mysqli->error;
return $error;
}
}
?>




That's it guys...



Comments
0

 

Announcement x
Dear Users and Clients,

This is to inform you that we have recently updated our website generally.

If you experience any problem viewing any part of this website or if you notice that any of our scripts or tutorials are not functioning properly as should be, please click on the Contact Link at the top of this page to report the issue to us.

Our priority is to give you the best of service!


Thank You,
The Vasplus Team

Announcement