Notes 004PHPPOD study notes 03 1. establish a connection
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
?>
2. connection error handling
Try {
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
Foreach ($ dbh-> query ('select * from Foo') as $ row ){
Print_r ($ row );
}
$ Dbh = null;
} Catch (PDOException $ e ){
Print "Error! : ". $ E-> getMessage ()."
";
Die ();
}
?>
Close connection
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
// Use the connection here
// Now the running is complete. close the connection here $ dbh = null;?>
Transactions
Not every Database supports transactions. Therefore, when a connection is opened for the first time, PDO needs to run in the so-called "automatic commit" mode. The automatic commit mode means that, if the database supports, each query that runs has its own implicit transactions. if the database does not support transactions, no. If you need a transaction, you must use the PDO: beginTransaction () method to start it. If the underlying driver does not support transactions, a PDOException exception is thrown (regardless of the error processing settings, this is a serious error state ). Once the transaction starts, you can use PDO: commit () or PDO: rollBack () to complete the task, depending on whether the code in the transaction is successful.
When the script ends or the connection is about to be closed, if there is still an unfinished transaction, PDO will automatically roll back the transaction. This security measure helps avoid inconsistencies when the script is accidentally terminated-if the transaction is not committed explicitly, it is assumed that an error occurs somewhere, so rollback is executed to ensure data security.
In the following example, assume that a group of entries is created for the new employee and an ID of 23 is assigned. In addition to registering the basic data of this person, you also need to record his salary. The two updates are easy to complete, but they are closed in the PDO: beginTransaction () and PDO: commit () calls to ensure that before the changes are completed, others cannot see these changes. If an error occurs, the catch block rolls back all the changes that have occurred since the transaction was started and outputs an error message.
Try {
$ Dbh = new PDO ('odbc: sample', 'db2inst1', 'ibmdb ',
Array (PDO: ATTR_PERSISTENT => true ));
Echo "Connected \ n ";
} Catch (Exception $ e ){
Die ("Unable to connect:". $ e-> getMessage ());
}
Try {
$ Dbh-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );
$ Dbh-> beginTransaction (); $ dbh-> exec ("insert into staff (id, first, last) values (23, 'job', 'bloggs ')"); $ dbh-> exec ("insert into salarychange (id, amount, changedate) values (23,500 00, NOW ()"); $ dbh-> commit ();
} Catch (Exception $ e) {$ dbh-> rollBack (); echo "Failed:". $ e-> getMessage () ;}?>
The above is the content of Note 004 php pod study note 03. For more information, see PHP Chinese website (www.php1.cn )!