How to Convert MySQL to MySQLi

2018.11.16 18:07

졸리운_곰 조회 수:96

 

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

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
98 PHP 리다이렉션(페이지 이동)의 3가지 방법 졸리운_곰 2019.01.11 151
97 mysql_ 함수에서 mysqli_ 함수로 변환 : How to Convert MySQL to MySQLi file 졸리운_곰 2019.01.05 101
96 PHP 의존성 관리도구 – Composer 시작하기 졸리운_곰 2018.12.28 88
95 PHP Mysqli 함수(사용빈도 높은것만..^^) 졸리운_곰 2018.12.25 142
94 Java 개발자로서 PHP 최단 시간에 공부하기 졸리운_곰 2018.11.21 114
93 Learn Blockchain In PHP 졸리운_곰 2018.11.18 121
92 MySQLConverterTool : How to Convert MySQL to MySQLi file 졸리운_곰 2018.11.16 86
» How to Convert MySQL to MySQLi file 졸리운_곰 2018.11.16 96
90 php game programming ebook 2004 : php 게임 프로그래밍 file 졸리운_곰 2018.10.15 81
89 PHP Composer 설치 및 사용법 졸리운_곰 2018.09.27 400
88 [PHP] PhpStorm IDE 사용하기 file 졸리운_곰 2018.07.19 167
87 phpStorm 원격 서버 연결 및 배포 (Deployment) file 졸리운_곰 2018.07.18 193
86 php 세션 저장소를 redis 로 바꾸어 본 후기. file 졸리운_곰 2018.05.05 467
85 아이오닉과 php 그리고 mysql 연동 : Using PHP and MySQL with Ionic file 졸리운_곰 2018.01.14 372
84 CKEditor 4 설치와 PHP 연동 하기 file 졸리운_곰 2018.01.14 277
83 5가지 조미료같은 워드프레스 플러그인 file 졸리운_곰 2018.01.14 84
82 wordpress 한시간만에 만들기 졸리운_곰 2017.10.26 99
81 PHP 프레임 새로고침할 때 페이지 유지하기 입니다. 졸리운_곰 2017.09.17 413
80 How to install composer on Wamp file 졸리운_곰 2017.09.10 148
79 Install Composer on Windows and WAMP Server file 졸리운_곰 2017.09.10 88
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED