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

WBOY
Release: 2016-07-13 17:47:44
Original
735 people have browsed it

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...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template