Loading.......
Loading...

How to change or convert MySQL to MySQLi

Posted on 23rd October, 2021
MYSQL TO MYSQLI CONVERT MYSQL TO MYSQLI MYSQL CONVERSION 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 your old mysql queries will continue to work as mysqli

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','chat'); 				//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;
	}
}
?>
Post Comment
Press Enter to send
No comment yet.


Popular Tutorials Pagination System using Ajax, Jquery and PHPSend Friend Request, Accept or Decline Request using Ajax, Jquery and PHPContact Form with Captcha using Ajax, Jquery and PHPWhy Having a FREE website is bad ChoiceBulk image resize with PHPWeb Browser Notification System using JavaScriptIntroduction to php.ini FileTypes of PHP ErrorsProgram Execution Time Limit in PHPTo-do list using PHP and MySQLiMove File from One Folder to the other using PHPHow to change or convert MySQL to MySQLiHow to Upload Video Files using PHP and MySQLiConvert HTML to MS Word Document using PHPEight Reasons Why You Need A Business WebsiteLocal Web Development Server for Windows - XAMPPEasy Shopping Cart using HTML, CSS & JavaScriptComment System using PHP, Ajax and MySQLiAdd, Edit and Delete Record from Database using jQuery and PHPResponsive Contact Form with Captcha using Jquery and PHPUpdate or Delete Multiple Rows using PHPPassword Strength Checker using JqueryFacebook Style Auto Scroll Pagination using jQuery and PHPDomain Checker using Ajax, Jquery and PHPThe if else & elseif Statements in PHPPreview and upload multiple files using ajax and PHPFancy Multiple File Upload using Ajax, Jquery and PHPPagination System without Database using Ajax and PHPUnderstanding Cookies in JavaScript: Create, Read, Update and DeleteSecured Users Access Level System using PHP and MysqlSecure Forgot Password System Using Ajax, Jquery and PHPChat Script Similar to Facebook using Ajax and PHPMake Dynamic XML sitemap using PHPHow to create an animated old letter using an animated penAuto-load User Details on Mouse-Over using Ajax, Jquery and PHPFacebook style Search People and Products using Ajax, Jquery and PHPAuto-suggestion using Ajax and PHPAuto-suggest System using Ajax, Jquery and PHPUsername Availability Checker using Ajax and PHPSimple PHP Anti-Spam Captcha Contact Form Submission

Submit your Job or Project Today!

We can help you turn your idea into reality, take over your existing project, or extend your current development team.

Submit your idea job or project below and we will follow up with you shortly.

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.

Please Wait....
Please Wait...