Front-end learning PDO basic operations of PHP
Dec 05, 2016 pm 01:26 PMPrevious words
PDO (php data object) extension class library defines a lightweight and consistent interface for PHP to access the database. It provides a database access abstraction layer so that no matter what database is used, queries can be executed through consistent functions. and obtaining data, which greatly simplifies the operation of the database and can shield the differences between different databases. Using PDO can easily carry out the development of cross-database programs and transplantation between different databases, which will be the main focus of PHP in database processing in the future. Development direction, it can support mysql, postgresql, oracle, mssql and other databases
Create PDO object
When using PDO to interact with different database management systems, the member methods in the PDO object are to unify the access interfaces of various databases, so before using PDO to interact with the database, you must first create a PDO object. While creating an object through the constructor method, you need to establish a connection with the database server and select a database
The prototype of PDO’s construction method is as follows
__construct ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$dsn</span> [,<span style="color: #0000ff;">string</span> <span style="color: #800080;">$username</span> [,<span style="color: #0000ff;">string</span> <span style="color: #800080;">$password</span> [,<span style="color: #0000ff;">array</span> <span style="color: #800080;">$driver_options</span> ]]] )
In the construction method, the first required parameter is the data source name (dsn), which is used to define a certain database and the driver that must be used. DSN's PDO naming convention is the name of the PDO driver, followed by a colon, and then optional driver database connection variable information, such as host name, port and database name
The second parameter username and the third parameter password in the construction method specify the username and password used to connect to the database respectively. The last parameter driver_options requires an array to specify all additional options required for the connection, passing additional tuning parameters to PDO or the underlying driver
<span style="color: #008000;">/*</span><span style="color: #008000;">連接如果失敗,使用異常處理模式進行捕獲 </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$dsn</span> = 'mysql:dbname=pdotest;host=127.0.0.1'; <span style="color: #008000;">//</span><span style="color: #008000;">連接MySQL數(shù)據(jù)庫的DSN </span> <span style="color: #800080;">$user</span> = 'root'; <span style="color: #008000;">//</span><span style="color: #008000;">MySQL數(shù)據(jù)庫的用戶名</span> <span style="color: #800080;">$password</span> = '*****'; <span style="color: #008000;">//</span><span style="color: #008000;">MySQL數(shù)據(jù)庫的密碼</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #800080;">$dbh</span> = <span style="color: #0000ff;">new</span> PDO(<span style="color: #800080;">$dsn</span>, <span style="color: #800080;">$user</span>, <span style="color: #800080;">$password</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">catch</span> (PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '數(shù)據(jù)庫連接失?。?' . <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); }</span>
When creating a PDO object, there are some options related to database connection. You can pass the necessary options to form the data to the fourth parameter driver_opts of the constructor to pass additional tuning parameters to PDO or the underlying driver. Program
PDO::ATTR_AUTOCOMMIT):<span style="color: #000000;"> PDO是否關閉自動提交功能 PDO</span>::ATTR_ERRMODE):<span style="color: #000000;"> 當前PDO的錯誤處理的模式 PDO</span>::ATTR_CASE):<span style="color: #000000;"> 表字段字符的大小寫轉: PDO</span>::ATTR_CONNECTION_STATUS):<span style="color: #000000;"> 與連接狀態(tài)相關特有信息: PDO</span>::ATTR_ORACLE_NULLS):<span style="color: #000000;"> 空字符串轉換為SQL的null PDO</span>::ATTR_PERSISTENT):<span style="color: #000000;"> 應用程序提前獲取數(shù)據(jù)大 PDO</span>::ATTR_SERVER_INFO):<span style="color: #000000;"> 與數(shù)據(jù)庫特有的服務器信 PDO</span>::ATTR_SERVER_VERSION):<span style="color: #000000;"> 數(shù)據(jù)庫服務器版本號信息 PDO</span>::ATTR_CLIENT_VERSION): 數(shù)據(jù)庫客戶端版本號信息
<span style="color: #008000;">//</span><span style="color: #008000;">設置持久連接的選項數(shù)組作為最后一個參數(shù),可以一起設置多個元素 </span> <span style="color: #800080;">$opt</span> = <span style="color: #0000ff;">array</span>(PDO::ATTR_PERSISTENT => <span style="color: #0000ff;">true</span><span style="color: #000000;">); </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #800080;">$db</span> = <span style="color: #0000ff;">new</span> PDO('mysql:dbname=pdotest;host=127.0.0.1','root','*****',<span style="color: #800080;">$opt</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">catch</span> (PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "數(shù)據(jù)庫連接失?。?" .<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); }</span>
Use PDO objects
Adjust the behavioral attributes of PDO
There are many properties in the PDO object that are used to adjust the behavior of PDO or obtain the underlying driver status. If you do not pass the attribute option as the last parameter in the constructor when creating a PDO object, you can also set and obtain the values ??of these attributes through the setAttribute() and getAttribute() methods in the PDO object after the object is created
PDO::getAttribute()
PDO::getAttribute() is used to retrieve the attributes of a database connection
<span style="color: #0000ff;">mixed</span> PDO::getAttribute ( int <span style="color: #800080;">$attribute</span> )
PDO::setAttribute()
PDO::setAttribute() is used to set attributes
bool PDO::setAttribute ( int <span style="color: #800080;">$attribute</span> , <span style="color: #0000ff;">mixed</span> <span style="color: #800080;">$value</span> )
<span style="color: #800080;">$dbh</span>->setAttribute(PDO::ATTR_ERRMODE, PDO::<span style="color: #000000;">ERRMODE_EXCEPTION); </span><span style="color: #008000;">//</span><span style="color: #008000;">$dbh->setAttribute(3,2); </span> <span style="color: #800080;">$dbh</span>->setAttribute(PDO::ATTR_AUTOCOMMIT,0);<span style="color: #008000;">//</span><span style="color: #008000;">$dbh->setAttribute(0,0); </span> <span style="color: #800080;">$dbh</span>->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::<span style="color: #000000;">FETCH_ASSOC); </span><span style="color: #008000;">//</span><span style="color: #008000;">$dbh->setAttribute(19,2); </span> <span style="color: #0000ff;">echo</span> "\nPDO是否關閉自動提交功能:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_AUTOCOMMIT); </span><span style="color: #0000ff;">echo</span> "\n當前PDO的錯誤處理的模式:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_ERRMODE); </span><span style="color: #0000ff;">echo</span> "\n表字段字符的大小寫轉換: ". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_CASE); </span><span style="color: #0000ff;">echo</span> "\n與連接狀態(tài)相關特有信息: ". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_CONNECTION_STATUS); </span><span style="color: #0000ff;">echo</span> "\n空字符串轉換為SQL的null:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_ORACLE_NULLS); </span><span style="color: #0000ff;">echo</span> "\n應用程序提前獲取數(shù)據(jù)大?。?quot;.<span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_PERSISTENT); </span><span style="color: #0000ff;">echo</span> "\n與數(shù)據(jù)庫特有的服務器信息:".<span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_SERVER_INFO); </span><span style="color: #0000ff;">echo</span> "\n數(shù)據(jù)庫服務器版本號信息:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_SERVER_VERSION); </span><span style="color: #0000ff;">echo</span> "\n數(shù)據(jù)庫客戶端版本號信息:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::ATTR_CLIENT_VERSION);
Error handling
PDO provides a total of three different error handling modes, which can not only meet different styles of programming, but also adjust and extend the way of handling errors
PDO:ERRORMODE_SILENT
This is the default mode, no action is taken when an error occurs, PDO will only set the error code. Developers can check statements and database objects through the errorCode() and errorInfo() methods in the PDO object. If the error occurs due to a call to a statement object, the errorCode() or errorInfo() method can be called on that statement object. If the error is caused by calling a database object, then the above two methods can be called on that database object
PDO:ERRMODE_WARNING
In addition to setting the error code, PDO will also emit a PHP traditional E_WARNING message, which can be caught using regular PHP error handlers. This setup is useful in debugging or testing if you just want to see what went wrong without inadvertently interrupting the flow of your application
<span style="color: #800080;">$dbh</span>->setAttrbute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);<span style="color: #008000;">//</span><span style="color: #008000;">設置警告模式處理錯誤</span>
PDO:ERRMODE_EXCEPTION
In addition to setting the error code, PDO will also throw a PDOException and set its properties to reflect the error code and error information. This setting is also useful in debugging, as it will zoom in on where in the script the error is occurring, making it possible to pinpoint problematic potential areas of the code very quickly. Another useful aspect of the exception pattern is that you can structure your own error handling more clearly than traditional PHP-style warnings, and rather than silently and explicitly checking the return value of each database call, the exception pattern Less code and nested code
<span style="color: #800080;">$dbh</span>->setAttrbute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);<span style="color: #008000;">//</span><span style="color: #008000;">設置異常模式處理錯誤</span>
執(zhí)行SQL語句
在使用PDO執(zhí)行查詢數(shù)據(jù)之前,先提供一組相關的數(shù)據(jù)。創(chuàng)建PDO對象并通過mysql驅動連接mysql數(shù)據(jù)庫服務器,創(chuàng)建一個以'testdb'命名的數(shù)據(jù)庫,并在該數(shù)據(jù)庫中創(chuàng)建一個聯(lián)系人信息表contactInfo
<span style="color: #000000;">CREATE TABLE contactInfo( uid MEDIUMINT(</span>8) UNSIGNED NOT <span style="color: #0000ff;">NULL</span> AUTO_INCREMENT,<span style="color: #000000;"> name VARCHAR(</span>50) NOT <span style="color: #0000ff;">NULL</span>,<span style="color: #000000;"> departmentID CHAR(</span>3) NOT <span style="color: #0000ff;">NULL</span>,<span style="color: #000000;"> address VARCHAR(</span>80) NOT <span style="color: #0000ff;">NULL</span>,<span style="color: #000000;"> phone VARCHAR(</span>20),<span style="color: #000000;"> email VARCHAR(</span>20),<span style="color: #000000;"> PRIMARY </span><span style="color: #008080;">KEY</span><span style="color: #000000;">(uid) );</span>
數(shù)據(jù)表contactInfo建立之后,向表中插入多行記錄
INSERT INTO contactInfo(name,departmentID,address,phone,email) VALUES ('張三','D01','朝陽','15011111234','zs@aaa.com'),('李四','D02','朝陽','15011112345','ls@aaa.com'),('王五','D02','海淀','15011113456','ww@aaa.com'),('趙四','D01','海淀','15011114567','zx@aaa.com');

PDO::exec()
PDO::exec()函數(shù)執(zhí)行一條SQL語句,并返回受影響的行數(shù)
int PDO::<span style="color: #008080;">exec</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$statement</span> )
當執(zhí)行INSERT、UPDATE、DELETET等沒有結果集的查詢時,使用PDO對象中的exec()方法去執(zhí)行。該方法成功執(zhí)行后,將返回受影響的行數(shù)
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">創(chuàng)建對象</span> <span style="color: #800080;">$dbh</span> = <span style="color: #0000ff;">new</span> PDO("mysql:host=localhost;dbname=testdb", "root", "zhiaihebe0123"<span style="color: #000000;">); }</span><span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "數(shù)據(jù)庫連接失敗:".<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } </span><span style="color: #800080;">$query</span> = "UPDATE contactInfo SET phone='12345678900' WHERE name='張三'"<span style="color: #000000;">; </span><span style="color: #800080;">$affected</span> = <span style="color: #800080;">$dbh</span>-><span style="color: #008080;">exec</span>(<span style="color: #800080;">$query</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$affected</span><span style="color: #000000;">){ </span><span style="color: #008000;">//</span><span style="color: #008000;">數(shù)據(jù)表contactInfo中受影響的行數(shù)為:1</span> <span style="color: #0000ff;">echo</span> '數(shù)據(jù)表contactInfo中受影響的行數(shù)為:' .<span style="color: #800080;">$affected</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$dbh</span>-><span style="color: #000000;">errorInfo()); } </span><span style="color: #800080;">$query</span> = "UPDATE contactInfo SET phone='123456789' WHERE (uid%2 = 0)"<span style="color: #000000;">; </span><span style="color: #800080;">$affected</span> = <span style="color: #800080;">$dbh</span>-><span style="color: #008080;">exec</span>(<span style="color: #800080;">$query</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$affected</span><span style="color: #000000;">){ </span><span style="color: #008000;">//</span><span style="color: #008000;">數(shù)據(jù)表contactInfo中受影響的行數(shù)為:2</span> <span style="color: #0000ff;">echo</span> '數(shù)據(jù)表contactInfo中受影響的行數(shù)為:' .<span style="color: #800080;">$affected</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$dbh</span>-><span style="color: #000000;">errorInfo()); } </span>?>

PDO::lastInsertId()
PDO::lastInsertId()函數(shù)用于返回最后插入行的ID或序列值
<span style="color: #0000ff;">string</span> PDO::lastInsertId ([ <span style="color: #0000ff;">string</span> <span style="color: #800080;">$name</span> = <span style="color: #0000ff;">NULL</span> ] )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">創(chuàng)建對象</span> <span style="color: #800080;">$dbh</span> = <span style="color: #0000ff;">new</span> PDO("mysql:host=localhost;dbname=testdb", "root", "zhiaihebe0123"<span style="color: #000000;">); }</span><span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "數(shù)據(jù)庫連接失?。?quot;.<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">try</span><span style="color: #000000;">{ </span><span style="color: #800080;">$query</span> = "INSERT INTO contactInfo(name,departmentID,phone,email) VALUES ('諸葛','D03','120120120','zg@aaa.com')"<span style="color: #000000;">; </span><span style="color: #800080;">$affected</span> = <span style="color: #800080;">$dbh</span>-><span style="color: #008080;">exec</span>(<span style="color: #800080;">$query</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$affected</span>."<br>";<span style="color: #008000;">//</span><span style="color: #008000;">1</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$dbh</span>->lastInsertId();<span style="color: #008000;">//</span><span style="color: #008000;">5</span> }<span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "錯誤:" .<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); } </span>?>

PDO::query()
當執(zhí)行返回結果集的SELECT查詢時,或者所影響的行數(shù)無關緊要時,應當使用PDO對象中的query()方法。如果該方法成功執(zhí)行指定的查詢,則返回一個PDOStatement對象。如果使用了query()方法,并想了解獲取的數(shù)據(jù)行總數(shù),可以使用PDOStatement對象中的rowCount()方法獲取
PDOStatement::rowCount()
PDOStatement::rowCount()函數(shù)返回受上一個 SQL 語句影響的行數(shù)
int PDOStatement::rowCount ( void )
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">創(chuàng)建對象</span> <span style="color: #800080;">$dbh</span> = <span style="color: #0000ff;">new</span> PDO("mysql:host=localhost;dbname=testdb", "root", "zhiaihebe0123"<span style="color: #000000;">); }</span><span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "數(shù)據(jù)庫連接失?。?quot;.<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } </span><span style="color: #800080;">$query</span> = "SELECT name,phone,email FROM contactInfo WHERE departmentId='D01'"<span style="color: #000000;">; </span><span style="color: #0000ff;">try</span><span style="color: #000000;">{ </span><span style="color: #800080;">$pdostatement</span> = <span style="color: #800080;">$dbh</span>->query(<span style="color: #800080;">$query</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "一共從表中獲取到".<span style="color: #800080;">$pdostatement</span>->rowCount()."條記錄:<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$pdostatement</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$row</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['name'] ."\t"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['phone'] ."\t"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['email'] ."<br>"<span style="color: #000000;">; } }</span><span style="color: #0000ff;">catch</span> (PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); } </span>?>

?
事務處理
事務是確保數(shù)據(jù)庫一致的機制,是一個或一系列的查詢,作為一個單元的一組有序的數(shù)據(jù)庫操作。如果組中的所有SQL語句都操作成功,則認為事務成功,事務則被提交,其修改將作用于所有其他數(shù)據(jù)庫進程。即使在事務的組中只有一個環(huán)節(jié)操作失敗,事務也不成功,則整個事務將被回滾,該事務中所有操作都被取消。事務功能是企業(yè)級數(shù)據(jù)庫的一個重要部分,因為很多業(yè)務過程都包括多個步驟。如果任何一個步驟失敗,則所有步驟都不應發(fā)生。事務處理有4個特征:原子性(Atomicity)、一致性(Consistency)、獨立性(Isolation)和持久性(Durability),即ACID。對于在一個事務中執(zhí)行的任何工作,即使它是分階段進行的,也一定可以保證該工作會安全地應用于數(shù)據(jù)庫,并且在工作被提交時,不會受到其他連接的影響
MySQL目前只有InnoDB和BDB兩個數(shù)據(jù)庫表類型才支持事務,兩個表類型具有相同的特性,InnoDB表類型具有比BDB還豐富的特性,速度更快,因此建議使用InnoDB表類型。創(chuàng)建InnoDB類型的表實際上與創(chuàng)建任何其他類型表的過程沒有區(qū)別,如果數(shù)據(jù)庫沒有設置為默認的表類型,只要在創(chuàng)建時顯式指定要將表創(chuàng)建為InnoDB類型
要實現(xiàn)事務處理,首先要使用InnoDB引擎
ALTER TABLE contactInfo engine=innodb;

在默認的情況下,MySQL是以自動提交(autocommit)模式運行的,這就意味著所執(zhí)行的每一個語句都將立即寫入數(shù)據(jù)庫中。但如果使用事務安全的表格類型,是不希望有自動 提交的行為的,所以要在當前的會話中關閉自動提交
SET AUTOCOMMIT = 0;<span style="color: #008000;">//</span><span style="color: #008000;">在當前的會話中關閉自動提交</span>
如果提交被打開了,必須開啟一個事務;如果自動提交是關閉的,則不需要使用這條命令,因為輸入一個SQL命令時,一個事務將自動啟動
START TRANSACTION;<span style="color: #008000;">//</span><span style="color: #008000;">開啟一個事務</span>
在完成了一組事務的語句輸入后,需要提交一個事務,該事務才能在其他會話中被其他用戶所見
COMMIT;<span style="color: #008000;">//</span><span style="color: #008000;">提交一個事務給數(shù)據(jù)庫</span>
如果改變注意,可以回滾到以前的狀態(tài)
ROOLBACK;<span style="color: #008000;">//</span><span style="color: #008000;">事務被回滾,所有操作都被取消</span>
事務處理完成后,再次開啟自動提交
SET AUTOCOMMIT = 1;

下面在PHP中進行事務處理操作,對張三和李四進行部門交換來輪崗培養(yǎng)
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">創(chuàng)建對象</span> <span style="color: #800080;">$dbh</span> = <span style="color: #0000ff;">new</span> PDO("mysql:host=localhost;dbname=testdb", "root", "zhiaihebe0123"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">設置錯誤使用異常的模式</span> <span style="color: #800080;">$dbh</span> -> setAttribute(PDO::ATTR_ERRMODE, PDO::<span style="color: #000000;">ERRMODE_EXCEPTION); </span><span style="color: #008000;">//</span><span style="color: #008000;">關閉自動提交</span> <span style="color: #800080;">$dbh</span>-> setAttribute(PDO::ATTR_AUTOCOMMIT, 0<span style="color: #000000;">); }</span><span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "數(shù)據(jù)庫連接失敗:".<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">開啟一個事務</span> <span style="color: #800080;">$dbh</span> -><span style="color: #000000;"> beginTransaction(); </span><span style="color: #800080;">$affected_rows</span> = <span style="color: #800080;">$dbh</span>-><span style="color: #008080;">exec</span>("UPDATE contactInfo set departmentID = 'D02' where uid=1"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$affected_rows</span> > 0<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "張三轉崗成功!<br>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> PDOException("張三轉崗失?。?lt;br>"<span style="color: #000000;">); } </span><span style="color: #800080;">$affected_rows</span> = <span style="color: #800080;">$dbh</span>-> <span style="color: #008080;">exec</span>("UPDATE contactInfo set departmentID = 'D01' where uid=2"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$affected_rows</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "李四轉崗成功!<br>"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> PDOException("李四轉崗失??!<br>"<span style="color: #000000;">); } </span><span style="color: #0000ff;">echo</span> "輪崗成功!<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">提交以上的操作</span> <span style="color: #800080;">$dbh</span>-><span style="color: #000000;">commit(); }</span><span style="color: #0000ff;">catch</span>(PDOException <span style="color: #800080;">$e</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "錯誤:".<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage(); </span><span style="color: #0000ff;">echo</span> "轉崗失敗!<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">撤銷所有操作</span> <span style="color: #800080;">$dbh</span> -><span style="color: #000000;"> rollback(); } </span><span style="color: #008000;">//</span><span style="color: #008000;">運行完成以后, 最后開啟自動提交</span> <span style="color: #800080;">$dbh</span>-> setAttribute(PDO::ATTR_AUTOCOMMIT, 1<span style="color: #000000;">); </span>?>


Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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)
