


PHP's mysql function imitates pdo to operate database class_PHP tutorial
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

不同数据库系统添加列的语法为:MySQL:ALTER TABLE table_name ADD column_name data_type;PostgreSQL:ALTER TABLE table_name ADD COLUMN column_name data_type;Oracle:ALTER TABLE table_name ADD (column_name data_type);SQL Server:ALTER TABLE table_name ADD column_name data_
