国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Table of Contents
Previous words
Create PDO object
Use PDO objects
事務(wù)處理
Home php教程 php手冊(cè) Front-end learning PDO basic operations of PHP

Front-end learning PDO basic operations of PHP

Dec 05, 2016 pm 01:26 PM

×
Table of Contents
[1] Create PDO [2] Use PDO [3] Transaction processing

Previous 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;">連接如果失敗,使用異常處理模式進(jìn)行捕獲 </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是否關(guān)閉自動(dòng)提交功能
 PDO</span>::ATTR_ERRMODE):<span style="color: #000000;"> 當(dāng)前PDO的錯(cuò)誤處理的模式 
 PDO</span>::ATTR_CASE):<span style="color: #000000;"> 表字段字符的大小寫轉(zhuǎn): 
 PDO</span>::ATTR_CONNECTION_STATUS):<span style="color: #000000;"> 與連接狀態(tài)相關(guān)特有信息: 
 PDO</span>::ATTR_ORACLE_NULLS):<span style="color: #000000;"> 空字符串轉(zhuǎn)換為SQL的null 
 PDO</span>::ATTR_PERSISTENT):<span style="color: #000000;"> 應(yīng)用程序提前獲取數(shù)據(jù)大 
 PDO</span>::ATTR_SERVER_INFO):<span style="color: #000000;"> 與數(shù)據(jù)庫特有的服務(wù)器信 
 PDO</span>::ATTR_SERVER_VERSION):<span style="color: #000000;"> 數(shù)據(jù)庫服務(wù)器版本號(hào)信息
 PDO</span>::ATTR_CLIENT_VERSION): 數(shù)據(jù)庫客戶端版本號(hào)信息 
<span style="color: #008000;">//</span><span style="color: #008000;">設(shè)置持久連接的選項(xiàng)數(shù)組作為最后一個(gè)參數(shù),可以一起設(shè)置多個(gè)元素 </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是否關(guān)閉自動(dòng)提交功能:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_AUTOCOMMIT);
</span><span style="color: #0000ff;">echo</span> "\n當(dāng)前PDO的錯(cuò)誤處理的模式:". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_ERRMODE); 
</span><span style="color: #0000ff;">echo</span> "\n表字段字符的大小寫轉(zhuǎ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)相關(guān)特有信息: ". <span style="color: #800080;">$dbh</span>->getAttribute(PDO::<span style="color: #000000;">ATTR_CONNECTION_STATUS); 
</span><span style="color: #0000ff;">echo</span> "\n空字符串轉(zhuǎ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應(yīng)用程序提前獲取數(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ù)庫特有的服務(wù)器信息:".<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ù)庫服務(wù)器版本號(hào)信息:". <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ù)庫客戶端版本號(hào)信息:". <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;">設(shè)置警告模式處理錯(cuò)誤</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;">設(shè)置異常模式處理錯(cuò)誤</span>

執(zhí)行SQL語句

  在使用PDO執(zhí)行查詢數(shù)據(jù)之前,先提供一組相關(guān)的數(shù)據(jù)。創(chuàng)建PDO對(duì)象并通過mysql驅(qū)動(dòng)連接mysql數(shù)據(jù)庫服務(wù)器,創(chuàng)建一個(gè)以'testdb'命名的數(shù)據(jù)庫,并在該數(shù)據(jù)庫中創(chuàng)建一個(gè)聯(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> )

  當(dāng)執(zhí)行INSERT、UPDATE、DELETET等沒有結(jié)果集的查詢時(shí),使用PDO對(duì)象中的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)建對(duì)象</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)建對(duì)象</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> "錯(cuò)誤:" .<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();
}
</span>?>

PDO::query()

  當(dāng)執(zhí)行返回結(jié)果集的SELECT查詢時(shí),或者所影響的行數(shù)無關(guān)緊要時(shí),應(yīng)當(dāng)使用PDO對(duì)象中的query()方法。如果該方法成功執(zhí)行指定的查詢,則返回一個(gè)PDOStatement對(duì)象。如果使用了query()方法,并想了解獲取的數(shù)據(jù)行總數(shù),可以使用PDOStatement對(duì)象中的rowCount()方法獲取

PDOStatement::rowCount()

  PDOStatement::rowCount()函數(shù)返回受上一個(gè) 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)建對(duì)象</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> = "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>?>

?

事務(wù)處理

  事務(wù)是確保數(shù)據(jù)庫一致的機(jī)制,是一個(gè)或一系列的查詢,作為一個(gè)單元的一組有序的數(shù)據(jù)庫操作。如果組中的所有SQL語句都操作成功,則認(rèn)為事務(wù)成功,事務(wù)則被提交,其修改將作用于所有其他數(shù)據(jù)庫進(jìn)程。即使在事務(wù)的組中只有一個(gè)環(huán)節(jié)操作失敗,事務(wù)也不成功,則整個(gè)事務(wù)將被回滾,該事務(wù)中所有操作都被取消。事務(wù)功能是企業(yè)級(jí)數(shù)據(jù)庫的一個(gè)重要部分,因?yàn)楹芏鄻I(yè)務(wù)過程都包括多個(gè)步驟。如果任何一個(gè)步驟失敗,則所有步驟都不應(yīng)發(fā)生。事務(wù)處理有4個(gè)特征:原子性(Atomicity)、一致性(Consistency)、獨(dú)立性(Isolation)和持久性(Durability),即ACID。對(duì)于在一個(gè)事務(wù)中執(zhí)行的任何工作,即使它是分階段進(jìn)行的,也一定可以保證該工作會(huì)安全地應(yīng)用于數(shù)據(jù)庫,并且在工作被提交時(shí),不會(huì)受到其他連接的影響

  MySQL目前只有InnoDB和BDB兩個(gè)數(shù)據(jù)庫表類型才支持事務(wù),兩個(gè)表類型具有相同的特性,InnoDB表類型具有比BDB還豐富的特性,速度更快,因此建議使用InnoDB表類型。創(chuàng)建InnoDB類型的表實(shí)際上與創(chuàng)建任何其他類型表的過程沒有區(qū)別,如果數(shù)據(jù)庫沒有設(shè)置為默認(rèn)的表類型,只要在創(chuàng)建時(shí)顯式指定要將表創(chuàng)建為InnoDB類型

  要實(shí)現(xiàn)事務(wù)處理,首先要使用InnoDB引擎

ALTER TABLE contactInfo engine=innodb;

  在默認(rèn)的情況下,MySQL是以自動(dòng)提交(autocommit)模式運(yùn)行的,這就意味著所執(zhí)行的每一個(gè)語句都將立即寫入數(shù)據(jù)庫中。但如果使用事務(wù)安全的表格類型,是不希望有自動(dòng) 提交的行為的,所以要在當(dāng)前的會(huì)話中關(guān)閉自動(dòng)提交

SET AUTOCOMMIT = 0;<span style="color: #008000;">//</span><span style="color: #008000;">在當(dāng)前的會(huì)話中關(guān)閉自動(dòng)提交</span>

  如果提交被打開了,必須開啟一個(gè)事務(wù);如果自動(dòng)提交是關(guān)閉的,則不需要使用這條命令,因?yàn)檩斎胍粋€(gè)SQL命令時(shí),一個(gè)事務(wù)將自動(dòng)啟動(dòng)

START TRANSACTION;<span style="color: #008000;">//</span><span style="color: #008000;">開啟一個(gè)事務(wù)</span>

  在完成了一組事務(wù)的語句輸入后,需要提交一個(gè)事務(wù),該事務(wù)才能在其他會(huì)話中被其他用戶所見

COMMIT;<span style="color: #008000;">//</span><span style="color: #008000;">提交一個(gè)事務(wù)給數(shù)據(jù)庫</span>

  如果改變注意,可以回滾到以前的狀態(tài)

ROOLBACK;<span style="color: #008000;">//</span><span style="color: #008000;">事務(wù)被回滾,所有操作都被取消</span>

  事務(wù)處理完成后,再次開啟自動(dòng)提交

SET AUTOCOMMIT = 1;

  下面在PHP中進(jìn)行事務(wù)處理操作,對(duì)張三和李四進(jìn)行部門交換來輪崗培養(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)建對(duì)象</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;">設(shè)置錯(cuò)誤使用異常的模式</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;">關(guān)閉自動(dòng)提交</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;">開啟一個(gè)事務(wù)</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> "張三轉(zhuǎn)崗成功!<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("張三轉(zhuǎn)崗失??!<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> "李四轉(zhuǎn)崗成功!<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("李四轉(zhuǎn)崗失?。?lt;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> "錯(cuò)誤:".<span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();
    </span><span style="color: #0000ff;">echo</span> "轉(zhuǎn)崗失敗!<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;">運(yùn)行完成以后, 最后開啟自動(dòng)提交</span>
<span style="color: #800080;">$dbh</span>-> setAttribute(PDO::ATTR_AUTOCOMMIT, 1<span style="color: #000000;">);
</span>?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)