Mysql functions in php perform database operations like pdo

Source: Internet
Author: User
Tags mysql functions php database

 

PDO will obviously become a standard php database operation method. Although many php space or hosts in China support php5.2 or later versions, some of them do not support pdo due to popularity and technical reasons.

 

Since the php course of Harbin Zhihua software was changed from studying database operations to focusing on pdo (of course, mySQL function operation database training was still carried out during the course study and final training) this year ), in actual development, my students inevitably encountered a program developed using pdo. During program implementation, they found that the server did not support pdo. Fortunately, they were all client servers, after upgrading and installing the new php version, you can solve the problem.

 

But this raises a question?

Either the mySQL function is used for development,

You can either use pdo for development and then upgrade the php version,

You can either find that you cannot upgrade the php version using pdo development, and rewrite the original pdo code using mySQL functions.

 

It seems that developing and using mySQL functions has become the best solution. This does not return to the starting point. Do you have to wait until pdo is popularized and then use pdo for development, this means that my php course will teach students a development method that has already been eliminated. After the students finish learning, they will have to give up the mySQL function development method they are used to in a few years, then adapt to the new pdo development method.

 

Is there a compromise that will allow students to learn about the new pdo development method and cope with the old mySQL function development method at work? On that day, the student asked me to write a class. The classes are all pdo methods, and then I can encapsulate all mySQL function operations into the class methods.

 

The original solution is that all development uses the pdo method. If you need the mySQL function method, you only need to replace the database connection file with the mySQL function code, then add this class to the back and instantiate the operation object whose object is pdo. You cannot change the pdo mode to the mySQL function mode.

 

The addition, deletion, and modification of pdo are all the same. The Code is as follows:

 

 

<? Php

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:

 

 

<? Php

Require './connDB. php ';

 

Require './Deep. Class. MySQLfunction4PDO. php ';

$ Db = new Deep_MySQLfunction4PDO ();

 

// 2 query

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

 

// 1 record

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

// Echo $ row ['guestname'], "<br/> ";

 

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

{

Echo $ row ['estname'], "<br/> ";

}

?>

 

 

 

You will find that I have added two lines of code after connecting to the database file (of course, the two lines are exactly written in the database file at work)

 

 

Require './Deep. Class. MySQLfunction4PDO. php ';

$ Db = new Deep_MySQLfunction4PDO ();

 

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

 

The code of the Deep_MySQLfunction4PDO class file is as follows:

 

 

<? Php

/*

An operation class that imitates a mysql function in the pdo mode.

Harbin Zhihua software training school Lu Haipeng 2011-11-25

 

Call: called after mysql database connection Function

Require './Deep. Class. MySQLfunction4PDO. php ';

$ Db = new Deep_MySQLfunction4PDO ();

 

*/

 

// Similar to PDO class

Class Deep_MySQLfunction4PDO {

 

// Insert, delete, and 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;

}

 

 

}

 

// PDOStatement class

Class Deep_MySQLfunction4PDO_stmt {

Var $ query;

Public function fetch (){

Return mysql_fetch_array ($ this-> query );

}

 

}

?>

 

 

 

Some of the courses in the past few years are mostly available on weekends. Some students have suggested that pdo-like classes do not use this while ($ row = $ stmt-> fetch () for queries ()) I can't use foreach ($ stmt as $ row). I changed this class to implement it. This is a bit odd about the code. I will sort it out later in this article.

 

By Lu Haipeng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.