Cause
I have never paid attention to database related knowledge
A few months ago, I accidentally opened the following piece of code:
Being complained about was a way of writing N years ago. Later, it was also necessary for learning, and mysql alone was no longer suitable. So I searched online for good methods, and PDO came to me.
Temptation
While browsing the Internet, I saw a passage:
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.
|
The PDO extension defines a lightweight, persistent interface for PHP to access databases. Each database driver that implements the PDO interface can express their respective characteristics in the form of regular expansion. Note: Using the PDO extension itself does not implement any database functions. You must use a specific database PDO driver to access the database. PDO provides a data access abstraction layer, which means that no matter which database you are using, you can use the same function to query and obtain data. PDO does not provide data extraction, it does not rewrite SQL statements, or mimic these functions. You need to use a full-blown extraction layer if you need it.
|
First meeting If a worker wants to do his job well, he must first sharpen his tools. The installation of php5
has been described in detail. Let’s talk about thephp.ini of pdo
Configuration of First, you need to know:
In php.ini, the semicolon represents a comment. Removing the preceding semicolon means that the function can be used
Ok, now open
php.ini
extension=php_pdo.dll extension=php_pdo_firebird.dll extension=php_pdo_informix.dll extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll extension=php_pdo_oci.dll extension=php_pdo_oci8.dll extension=php_pdo_odbc.dll extension=php_pdo_pgsql.dll extension=php_pdo_sqlite.dll |
extension=php_pdo.dll extension=php_pdo_firebird.dll extension=php_pdo_informix.dll extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll extension=php_pdo_oci.dll extension=php_pdo_oci8.dll extension=php_pdo_odbc.dll extension=php_pdo_pgsql.dll extension=php_pdo_sqlite.dll |
Just remove the semicolon in front (I have already removed it here).
Be careful when configuring php.ini and back it up before modifying it. I once corrected a mistake and made php unable to work. I still didn’t know what the problem was. .
In this way, pdo is installed
Play with it
The first thing is of course to see how to connect to the database
It has a PDO class, which says to create a new pdo object, which should be This is it
Look at the grammar, the first statement comes out~(≧▽≦)/~
In it, mysql is the database type, localost is the host name, test is the database name, dbuser is the user name, dbpass is the password
Please note that try/catch code blocks, you should always encapsulate your PDO operations Within a try/catch block and use exception mechanism
Below is a netizen’s sentence diagram, which I put together into one sentence
The following is error handling
PDO provides different error handling strategies in 3.
PDO::ERRMODE_SILENT This is the default mode. PDO will set simple error codes on the statement anddatabase objects , you can use the PDO->errorCode()and PDO->errorInfo() methods to check for errors; if the error is Caused when callingstatement object, you can use PDOStatement->errorCode() orPDOStatement->errorInfo() method to obtain error information. And if the error is caused when calling the database object, you should call those two methods on this
|
id | name |
1 | aaa |
2 | bbb |
Data query
We want to query the data of id for 1
MYSQLVersion:
PDO Version:
Hey, it doesn’t look much different~~
Data update
We want to update the data of id to 1, name is changed to ccc
MYSQLVersion:
PDO Version:
I feel like thisexecis much more refreshing
Delete data
We want to delete the data for 1 MYSQL
Version:
PDO
Version:
Haha, still
execreturns the number of rows
Insert dataWe want to insert data
id = 3 , name = ccc MYSQL
Version:
PDO
Version:
Fun
exec() This function has appeared many times above, now I will briefly introduce it
PDO::exec()
Execute a SQL statement in a single function call, and the return is affected by this statement number of rows. PDO::exec()
will not return results from a SELECT statement. For SELECT statements that only need to be issued once in the program, consider using PDO::query(). For statements that need to be issued multiple times, you can use PDO::prepare() to prepare a PDOStatement object and use PDOStatement::execute() Issue a statement.
Summary
Although this technology is a bit old, it is still vibrant compared to the old sentences. As the study progressed, Loulou also learned a lot of things not mentioned in this article. Thank you everyone
~(≧▽≦)/~
These and many more. . . All operations involving the database have basically been replaced by
PDO I’m so happy
Technology is dead, but people are alive. Leverage old technology to create a new future.
本文参考资料:
版权所有
http://www.bkjia.com/PHPjc/923412.htmlwww.bkjia.com
References for this article: