Loading...
 
Gmail / Facebook Style Chat Application


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...


Views Today: 1
Total Views: 2330

Comments
0
OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Copyright © 2011 - 2024 | All Rights Reserved