PHP MySQL function Imitation PDO operation database class _php tutorial

Source: Internet
Author: User
Tags php mysql
PDO will obviously become a standard database operation for PHP. Although many of the domestic PHP space or host has supported php5.2 above version, but because of the popularity and technical reasons or some do not support PDO.

Since the beginning of this year, Harbin Zhihua software PHP course in the database operation of the study changed to PDO-based (course in the course of learning and final training or MySQL function operation database), in the actual development, my students inevitably encountered the use of PDO development program, In the implementation of the program but found that the server does not support PDO, fortunately, is the client's server, re-upgrade installed a new version of PHP to solve.

But this raises a question?

or develop a MySQL function,

Either develop the PDO and then upgrade the PHP version,

Either found using PDO development unable to upgrade PHP version, using the MySQL function to rewrite the original PDO mode code

It seems that the development of the use of MySQL function method seems to be the best solution, this does not go back to the starting point, do not have to wait until the PDO has been popularized to use PDO development, if this means that my PHP course will teach students a development method has been phased out, students after learning, In a few years you have to give up the habits of MySQL function development, and then adapt to the new PDO development method.

Is there a compromise that allows students to learn about the new PDO development approach, while at work they can cope with the old MySQL function development? That day the students asked the sudden whim to write a class, the method of the class is the way of PDO, and then the operation of the MySQL function is encapsulated in the method of this class can not be.

The solution is that the development is all using the PDO mode operation, if the need to MySQL function mode as long as the library to replace the file with the MySQL function library code, and then add this class and instantiate the object as PDO of the Operation object can be implemented to change the PDO mode to MySQL function mode

PDO additions and deletions 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 queries

$stmt = $db->query ("SELECT * from Guestbook");

1 Records

$row = $stmt->fetch ();

echo $row [' Guestname '], "
";

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

{

echo $row [' Guestname '], "
";

}

?>

You'll find me adding 2 lines of code after the library file (and of course working on the two lines in the library file)

Require './deep.class.mysqlfunction4pdo.php ';

$db = new Deep_mysqlfunction4pdo ();

So through the Deep_mysqlfunction4pdo class I realized that without changing the existing PDO code, the implementation of the MySQL function mode of operation

The Deep_mysqlfunction4pdo class file code is as follows:

/*

The operation class of a MySQL function that mimics the PDO way.

Harbin Zhihua Software Training School Lu Haipeng 2011-11-25

Call: Called after MySQL library function

Require './deep.class.mysqlfunction4pdo.php ';

$db = new Deep_mysqlfunction4pdo ();

*/

Imitation PDO class

Class deep_mysqlfunction4pdo{

Insert Delete Modification

Public function exec ($sql) {

@mysql_query ($sql) or Die ("SQL statement execution Error! ");

return Mysql_affected_rows ();

}

Inquire

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);

}

}

?>

A period of time the course is more than a few days of the weekend to organize records as above, and some students have proposed to imitate the PDO class encountered query do not use this while ($row = $stmt->fetch ()) with foreach ($stmt as $row) No, I changed this class to implement, This is the code, it's a little weird. I'm free to sort out the follow-up instructions for this post.

Author Lu Haipeng

http://www.bkjia.com/PHPjc/478490.html www.bkjia.com true http://www.bkjia.com/PHPjc/478490.html techarticle PDO will obviously become a standard database operation for PHP. Although many of the domestic PHP space or host has supported php5.2 more than the version, but because of the popularity and technical reasons are still part ...

  • Related Article

    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.