How to Convert MySQL to MySQLi

2018.11.16 18:07

졸리운_곰 조회 수:50

 

How to Convert MySQL to MySQLi

In PHP 7, the MySQL extension is completely removed. Learn how to easily convert a MySQL extension into MySQLi without it.

  · Web Dev Zone · Tutorial
 

 Comment (0)

 
Save
 
 29.25k Views
 
Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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.

Image title

In PHP 7, the MySQL extension is completely removed. Thus, in this article, 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.

MySQL to MySQLi Procedural Methods

I will first start with the most commonly used MySQL methods. The only thing that changed in the following method is the addition of "i" in MySQL and some changes of the positions of link and result in the parameter of the method.

Following are the popularly used methods of MySQL, followed by the equivalent methods in MySQLi.

 
mysql_affected_rows -> mysqli_affected_rows($link)
 
 
 
mysql_close -> mysqli_close($link)
 
 
 
mysql_data_seek -> mysqli_data_seek( $result, $offset)
 
 
 
mysql_errno -> mysqli_errno( $link)
 
 
 
mysql_error -> mysqli_error( $link)
 
 
 
mysql_fetch_array -> mysqli_fetch_array( $result, $type)
 
 
 
mysql_fetch_assoc -> mysqli_fetch_assoc( $result)
 
 
 
mysql_fetch_lengths -> mysqli_fetch_lengths( $result )
 
 
 
mysql_fetch_object -> mysqli_fetch_object( $result, $class, $params)
 
 
 
mysql_fetch_row -> mysqli_fetch_row( $result)
 
 
 
mysql_field_seek -> mysqli_field_seek( $result, $number)
 
 
 
mysql_free_result -> mysqli_free_result(result)
 
 
 
mysql_get_client_info -> mysqli_get_client_info( $link)
 
 
 
mysql_get_host_info -> mysqli_get_host_info( $link)
 
 
 
mysql_get_proto_info -> mysqli_get_proto_info( $link)
 
 
 
mysql_get_server_info -> mysqli_get_server_info( $link)
 
 
 
mysql_info -> mysqli_info( $link)
 
 
 
mysql_insert_id -> mysqli_insert_id( $link)
 
 
 
mysql_num_rows ->  mysqli_num_rows( $result)
 
 
 
mysql_ping -> mysqli_ping( $link)
 
 
 
mysql_query -> mysqli_query( $link, $query)
 
 
 
mysql_real_escape_string -> mysqli_real_escape_string( $link)
 
 
 
mysql_select_db - > mysqli_select_db( $link, $database)
 
 
 
mysql_set_charset -> mysqli_set_charset( $link, $charset)
 
 
 
mysql_stat -> mysqli_stat( $link)
 
 
 
mysql_thread_id -> mysqli_thread_id( $link)
 

If you want to convert your script from a MySQL extension to a MySQLi extension manually, you must start with the top of the script and start converting all the methods one by one.

For this, open the script in any text editor and use the find-and-replace tool to replace mysql_ with mysqli. But that's not all. You must manually check and verify the parameters of the method, as well.

To automate the process of converting the script from MySQL to MySQLi, I can use the following online tools. I will create a sample insertion and retrieval code and convert it using one of these tools.

Sample Code for Conversion

I am going to use the following database and code for testing the online conversion tools.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Database SQL

 
CREATE TABLE `info`.`student` 
 
  ( 
 
     `id`        INT NOT NULL auto_increment, 
 
     `name`      VARCHAR(40) NOT NULL, 
 
     `last name` VARCHAR(40) NOT NULL, 
 
     `email`     VARCHAR(40) NOT NULL, 
 
     PRIMARY KEY (`id`) 
 
  ) 
 
engine = innodb;
 

Connection.php

 
<?php
 
$servername = "localhost" ;
 
$username = "root";
 
$pass = "";
 
$con = mysql_connect($servername,$username,$pass) or die("Problem occur in connection");
 
$db = mysql_select_db("info",$con);
 
?>
 

Insert.php

 
<?php
 
require_once("connection.php");
 
$query = "Insert into student values('','Saquib','Rizwan','saquib.rizwan@cloudways.com')";
 
$result = mysql_query($query);
 
if(isset($result))
 
{
 
 echo "Data has been inserted";
 
}
 
?>
 

Retrieve.php

 
<?php
 
require_once("connection.php");
 
$query = "select * from Student ";
 
$result = mysql_query($query);
 
while($data = mysql_fetch_array($result))
 
{
 
 echo "<br/><br/>";
 
 echo "ID : $data[0]";
 
 echo "Name : $data[1]  ";
 
 echo "Last Name : $data[2]  ";
 
 echo "Email : $data[3]  ";
 
}
 
?>
 

Here is the basic PHP code which uses MySQL extension to create the database connection, select database, create and execute a query and fetch data from the database.

Now I will convert the script using an online tool.

ICT Academie

In order to convert the code using ICT Academie, all you need to do is paste your code snippet enclosed in PHP opening and closing tags.

For example, if you want to see how mysql_connect() gets converted, paste <?php mysql_connect() ?>>".

Code conversion result from the ICT Academie for the above code is as follows.

Connection.php

 
<?php  
 
$servername = "localhost" ;  
 
$username = "root"; 
 
$pass = "";  
 
$con = ($GLOBALS["___mysqli_ston"] = mysqli_connect($servername, $username, $pass)) or die("Problem occur in connection");  
 
$db = ((bool)mysqli_query($con, "USE " . info));  
 
?>
 

Insert.php

 
<?php  
 
require_once("connection.php"); 
 
$query = "Insert into student values('','Saquib','Rizwan','saquib.rizwan@cloudways.com')";  
 
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);  
 
if(isset($result)) 
 
{ 
 
    echo "Data has been inserted";  
 
} 
 
?>
 

Retrieve.php

 
<?php  
 
require_once("connection.php"); 
 
$query = "select * from Student ";  
 
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);  
 
while($data = mysqli_fetch_array($result)) 
 
{ 
 
    echo "<br/><br/>";  
 
    echo "ID : $data[0]    "; 
 
    echo "Name : $data[1]  "; 
 
    echo "Last Name : $data[2]  "; 
 
    echo "Email : $data[3]  ";  
 
} 
 
?>
 

To test the converted code, it is clear that all the MySQL methods have been successfully converted into MySQLi. It only changes the PHP code, but the result remains the same.

Note: Before starting the conversion process, create backups of all the files and test run all the code before live deployment. The converter only converts the code. It does not add any security measures to prevent SQL injections.

 

[출처] https://dzone.com/articles/convert-mysql-to-mysqli

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
108 [php machine learing] 회귀 분석 phpml 졸리운_곰 2020.02.06 88
107 [php machine learning] PHP-ML을 이용한 분류 졸리운_곰 2020.02.06 153
106 PHP-CRUD-API [mysql 및 데이터베이스 자동 미들웨어(middleware) by php] file 졸리운_곰 2020.01.25 41
105 Docker를 이용하여 멀티플랫폼에서 MSSQL + Laravel을 개발해보자. file 졸리운_곰 2020.01.23 140
104 PHP 기반의 Micro Frameworks 정리 졸리운_곰 2019.11.19 40
103 Micro-Framework, Lumen 살펴보기 file 졸리운_곰 2019.11.19 20
102 라라벨-루멘-PHP 날코딩 성능 비교 file 졸리운_곰 2019.11.19 113
101 Using WordPress with Lumen 졸리운_곰 2019.11.06 125
100 Micro-Framework, Lumen 살펴보기 file 졸리운_곰 2019.11.05 67
99 PHP 로 guid(uuid) 만들기 졸리운_곰 2019.02.27 716
98 PHP 리다이렉션(페이지 이동)의 3가지 방법 졸리운_곰 2019.01.11 94
97 mysql_ 함수에서 mysqli_ 함수로 변환 : How to Convert MySQL to MySQLi file 졸리운_곰 2019.01.05 70
96 PHP 의존성 관리도구 – Composer 시작하기 졸리운_곰 2018.12.28 47
95 PHP Mysqli 함수(사용빈도 높은것만..^^) 졸리운_곰 2018.12.25 96
94 Java 개발자로서 PHP 최단 시간에 공부하기 졸리운_곰 2018.11.21 64
93 Learn Blockchain In PHP 졸리운_곰 2018.11.18 89
92 MySQLConverterTool : How to Convert MySQL to MySQLi file 졸리운_곰 2018.11.16 52
» How to Convert MySQL to MySQLi file 졸리운_곰 2018.11.16 50
90 php game programming ebook 2004 : php 게임 프로그래밍 file 졸리운_곰 2018.10.15 48
89 PHP Composer 설치 및 사용법 졸리운_곰 2018.09.27 288
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED