Home >Backend Development >PHP Tutorial >Detailed explanation of the use of PDO mysql connection method in php_PHP tutorial
This article mainly introduces in detail the various usage methods of PDO, the mysql connection method in PHP. It is a personal summary. If there are any omissions Please also let me know so that friends in need can refer to it.
Common methods of PDO:
PDO::query() is mainly used for operations that return recorded results (PDOStatement), especially select operations.
PDO::exec() is mainly for operations that do not return a result set. Such as insert, update and other operations. Returns the number of affected rows.
PDO::lastInsertId() returns the last ID of the last insertion operation, but please note: if you use insert into tb(col1,col2) values(v1,v2),(v11,v22).. to insert multiple records at one time , lastinsertid() returns only the ID of the first record (v1, v2) inserted, not the record ID of the last record inserted.
PDOStatement::fetch() is used to obtain a record. Use while to traverse.
PDOStatement::fetchAll() fetches all records into one.
PDOStatement::fetchcolumn([int column_indexnum]) is used to directly access the column. The parameter column_indexnum is the index value of the column in the row starting from 0. However, this method can only obtain one column of the same row at a time. As long as it is executed once, it will jump. Go to the next line. Therefore, it is easier to use when directly accessing a certain column, but it is not useful when traversing multiple columns.
PDOStatement::rowcount() is suitable for obtaining the number of records when using the query("select...") method. It can also be used in preprocessing. $stmt->rowcount();
PDOStatement::columncount() is suitable for obtaining the number of columns in a record when using the query("select...") method.
Note:
1. Choose fetch or fetchall?
When the record set is small, using fetchall is efficient and reduces the number of retrieval times from the database. However, for large result sets, using fetchall brings a lot of burden to the system. The amount of data the database needs to transmit to the WEB front-end is too large and inefficient.
2. fetch() or fetchall() has several parameters:
mixed pdostatement::fetch([int fetch_style [,int cursor_orientation [,int cursor_offset]]])
array pdostatement::fetchAll(int fetch_style)
fetch_style parameter:
■$row=$rs->fetchAll(PDO::FETCH_BOTH); FETCH_BOTH is the default, can be omitted, and returns the association and index.
■$row=$rs->fetchAll(PDO::FETCH_ASSOC); The FETCH_ASSOC parameter determines that only associative arrays are returned.
■$row=$rs->fetchAll(PDO::FETCH_NUM); Return index array
■$row=$rs->fetchAll(PDO::FETCH_OBJ); If fetch() returns an object, if fetchall() returns a two-dimensional array composed of objects
Copy the code The code is as follows:
1 Establish connection
The code is as follows:
Persistence link PDO::ATTR_PERSISTENT=>true
2. Catching errors
The code is as follows:
3. Business
The code is as follows:
4. Error handling
a. Silent mode (default mode)
The code is as follows:
The code is as follows:
1. Use query()
The code is as follows:
2. Use prepare, bindParam and execute [recommended, you can also add, modify, delete]
Copy the code The code is as follows:
PDOStatement->bindParam()
PDOStatement->execute();//You can put the corresponding bound variables here
?>
3. Things
Copy the code The code is as follows:
The above is all about the usage of pdo in PHP. I hope this article can be helpful to everyone, and I hope everyone likes it.