


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

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
