Home Backend Development PHP Tutorial PHP's mysql function imitates pdo to operate database class_PHP tutorial

PHP's mysql function imitates pdo to operate database class_PHP tutorial

Jul 13, 2016 pm 05:47 PM
mysql pdo php function Will become operate database Way standard of kind

PDO will obviously become the standard database operation method for PHP. Although many php spaces or hosts in China already support php5.2 or above, some do not support pdo due to popularity and technical reasons.

Since this year, Harbin Zhihua Software's PHP course has changed to focus on PDO in database operations (of course, the course still has mySQL function database training during learning and final training). In actual development, my students Inevitably, I encountered a program developed using pdo. When the program was implemented, I found that the server did not support pdo. Fortunately, it was the customer's server. I upgraded and installed a new version of php and the problem was solved.

But this raises a question?

Or develop using mySQL function,

Or use pdo for development and then upgrade the php version,

Or you find that you cannot upgrade the php version using pdo development, and use the mySQL function method to rewrite the original pdo method code

It seems that the mySQL function method for development has become the best solution. Doesn't this return to the starting point? Do we have to wait until pdo is popularized before using pdo development? If this means that my php course Students will be taught a development method that has begun to be eliminated. After students finish learning, they will have to give up the mySQL function development method they are accustomed to in a few years and adapt to the new pdo development method.

Is there a compromise solution that allows students to learn the new pdo development method while also dealing with the old mySQL function development method at work? The student asked that day that he suddenly wanted to write a class. The methods of the class are all in the pdo method, and then the operations of the mySQL functions can be encapsulated into the methods of this class.

The original intention of the solution is to use the pdo method for development. If you need the mySQL function method, just replace the library connection file with the mySQL function library code, then add this class at the end and instantiate the operation object whose object is pdo. The pdo implementation method has been changed to mySQL function method

The additions, deletions and modifications of pdo are the same, the code is as follows:

require "./connDB.php" ;

require ’./Deep.Class.MySQLfunction4PDO.php’;

$db = new Deep_MySQLfunction4PDO();

$sql="insert into guestbook(guestName,guestDatetime)values(’lvhaipeng zhihuasoft".mt_rand(1,100)." ’,’".date(’Y-m-d H:i:s’)."’)";

//2 exec method

$count = $db->exec($sql);

echo $count;

?>

The query code is as follows:

require ’./connDB.php’;

require ’./Deep.Class.MySQLfunction4PDO.php’;

$db = new Deep_MySQLfunction4PDO();

//2 query query

$stmt = $db->query("SELECT * FROM guestbook" );

// 1 record

// $row =$stmt->fetch();

//echo $row[’guestName’],"
";

while($row = $stmt->fetch())

{

echo $row[’guestName’],"
";

}

?>

You will find that I added 2 lines of code after the library file (of course, it is just right to write these two lines in the library file when working)

require ’./Deep.Class.MySQLfunction4PDO.php’;

$db = new Deep_MySQLfunction4PDO();

In this way, through the Deep_MySQLfunction4PDO class, I can achieve MySQL function operation without changing the existing pdo code

The Deep_MySQLfunction4PDO class file code is as follows:

/*

An operation class of mysql function that imitates the pdo method.

Harbin Zhihua Software Training School Lu Haipeng2011-11-25

Call: Call after the mysql library function

require ’./Deep.Class.MySQLfunction4PDO.php’;

$db = new Deep_MySQLfunction4PDO();

*/

// Imitation PDO class

class Deep_MySQLfunction4PDO{

//Insert Delete Modify

public function exec($sql){

@mysql_query( $sql ) or die("SQL statement execution error!");

return mysql_affected_rows();

}

//Query

public function query($sql){

$stmt=new Deep_MySQLfunction4PDO_stmt();

$stmt->query=@mysql_query( $sql ) or die("SQL statement execution error!");

return $stmt;

}

}

// Imitation PDOStatement class

class Deep_MySQLfunction4PDO_stmt{

var $query;

public function fetch(){

return mysql_fetch_array($this->query);

}

}

?>

There were a lot of courses some time ago, so I have time this weekend to sort out the records as above. Another student suggested that when encountering queries in imitating pdo classes, we should not use this while($row =$stmt->fetch()) but use foreach($stmt as $row) No, I changed the implementation of this class. This is a bit weird in how the code is written. I will sort it out and make a follow-up explanation of this article when I have time

Author Lu Haipeng

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478490.htmlTechArticlePDO will obviously become the standard database operation method for PHP. Although many domestic php spaces or hosts already support php5.2 or above, due to popularity and technical reasons, there are still some...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

Detailed explanation of the installation steps of MySQL on macOS system Detailed explanation of the installation steps of MySQL on macOS system Apr 29, 2025 pm 03:36 PM

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

How to analyze the execution plan of MySQL query How to analyze the execution plan of MySQL query Apr 29, 2025 pm 04:12 PM

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

See all articles