Home >php教程 >php手册 >Mysqli operates the database, mysqli operates the database

Mysqli operates the database, mysqli operates the database

WBOY
WBOYOriginal
2016-07-06 14:25:201112browse

Mysqli operates the database, mysqli operates the database

Every time you use mysql_connect to connect to the database, you will be prompted:

<span>1</span> Deprecated: <span>mysql_connect</span>(): The <span>mysql</span> extension is deprecated and will be removed in the future: <span>use</span> mysqli or PDO instead in E:\wamp\www\myquote\includes\<span>mysql_connect</span>.php on line 5

As a man with high demands and perfection, I really can’t stand this reminder. You can't even hide from him - only by solving the little stone in his heart can it be broken

Then first learn to use Mysqli to operate the database:

1.  When interacting with a Mysql database, the connection must be established first and the connection must be disconnected last. This includes connecting to the server and selecting a database, and finally closing the connection. As with almost all features of MySQL, this can be accomplished using an object-oriented approach or a procedural approach.

1.1 Create a Mysqli object

<span>1</span> <?<span>php
</span><span>2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span>3</span> ?>

1.2 Host, user, password, database connected to Mysql

<span>1</span> <?<span>php
</span><span>2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span>3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span>4</span> ?>

1.3 Create a Mysqli object for connection parameters

<span>1</span> <?<span>php
</span><span>2</span> <span>$mysqli</span>=<span>new</span> mysqli('localhost','root','admin','test');<span>//</span><span>实例化mysqli
</span><span>3</span> <span>//$mysqli->connect('localhost','root','admin','test');</span>
<span>4</span> ?>

1.4 Change the default connection database

<span>1</span> <?<span>php
</span><span>2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span>3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span>4</span> <span>$mysqli</span>->select_db('abc');<span>//</span><span>更换连接默认的数据库为abc数据库</span>
<span>5</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>6</span> ?>

2Handling connection errors

2.1 If the MySQL database cannot be connected, it is unlikely that the page will continue to do its intended job, so be sure to monitor connection errors and react accordingly. The Mysqli extension contains many features that can be used to capture error information, such as the mysqli_connect_errno() and mysqli_connect_error() methods.

<span>1</span> <span>mysqli_connect_errno</span><span>()函数返回链接数据库返回的错误号。
</span><span>2</span> <span>mysqli_connect_error</span><span>()函数返回连接数据库返回的错误代码。</span>

How to use:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>echo</span>'数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>();
</span><span> 6</span>     <span>exit</span><span>();
</span><span> 7</span> }<span>else</span><span>{
</span><span> 8</span>     <span>echo</span> '数据库连接成功'<span>;
</span><span> 9</span> <span>}
</span><span>10</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>11</span> ?>

A process-oriented method is used here to determine whether the database connection is successful. So why not use object-oriented method to judge? My understanding is that before judging whether the database connection is successful, you must first prove whether the object you want to instantiate is available. . Therefore, a process-oriented approach is used to judge. (If there is something wrong, you can discuss it together. I am also a scumbag in elementary school)

2.2 mysqli_error——Returns database operation error information. mysqli_errno——Return database operation error code

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>echo</span>'数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>();
</span><span> 6</span>     <span>exit</span><span>();
</span><span> 7</span> }<span>else</span><span>{
</span><span> 8</span>     <span>echo</span> '数据库连接成功<br/>'<span>;
</span><span> 9</span> <span>}
</span><span>10</span> <span>if</span> (!<span>$mysqli</span>->query("SET a=1")) {<span>//</span><span>$mysqli->query()执行sql语句</span>
<span>11</span>     <span>echo</span> "错误信息是: ".<span>$mysqli</span>->error.'<br/>'<span>;
</span><span>12</span>     <span>echo</span> "错误代码是: ".<span>$mysqli</span>-><span>errno;
</span><span>13</span> <span>}
</span><span>14</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>15</span> ?>

Output:

Database connection successful
The error message is: Unknown system variable 'a'
The error code is: 1193

3 Interact with the database.

① The vast majority of queries are related to creation (Creation), retrieval (Retrieval), update (Update) and deletion (Deletion) tasks. These tasks are collectively called CRUD.

② Obtaining data Most of the work of web page programs is to obtain and format the requested data. To do this, you need to send a SELECT query to the database, then iteratively process the results, output each row to the browser, and output it according to your own requirements.

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>print_r</span>(<span>$result</span>->fetch_row());<span>//</span><span>将结果集的第一行输出</span>
<span>11</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>12</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>13</span> ?>

Output:

Array ( [0] => 1 [1] => Akita Jia [2] => 123456 )

3.1 Parsing query results

Once the query has been executed and the result set has been prepared, it is time to parse the resulting rows. You can use multiple methods to get the fields in each row. Which method you choose mainly comes down to personal preference, because only the method of referencing the fields differs.

3.1.1 Put the result set into an object. Since you may use the object-oriented syntax of mysqli, you can completely manage the results in an object-oriented way. Fruit set.

This can be done using the fetch_object() method.

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>/*</span>
<span>11</span> <span>$row = $result->fetch_object();//将结果集包装成对象
</span><span>12</span> <span>echo $row->user;//输出对象中的一个字段(属性)
</span><span>13</span> <span>*/</span>
<span>14</span> <span>while</span> (!!<span>$row</span> = <span>$result</span>->fetch_object()){<span>//</span><span>//遍历所有的用户名称</span>
<span>15</span> <span>echo</span> <span>$row</span>->user."<br>"<span>;
</span><span>16</span> <span>var_dump</span>(<span>$row</span><span>);
</span><span>17</span> <span>}
</span><span>18</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>19</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>20</span> ?>

Output:

Akita Kazuo

object(stdClass)#3 (3) { ["id"]=> string(1) "1" ["user"]=> string(9) "Akita Ka" ["psw"]= > string(6) "123456" }
Andy Lau

object(stdClass)#4 (3) { ["id"]=> string(1) "2" ["user"]=> string(9) "Andy Lau" ["psw"]=> ; string(6) "654321" }

3.1.2 Using indexed arrays and associative arrays fetch_array();

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> 
<span>11</span> <span>$row</span> = <span>$result</span>->fetch_array();<span>//</span><span>将结果集包装成数组(索引+关联)</span>
<span>12</span> <span>echo</span> <span>$row</span>[1].'<br/>';<span>//</span><span>输出下标是1的字段(属性)</span>
<span>13</span> <span>var_dump</span>(<span>$row</span><span>);
</span><span>14</span> <span>/*</span>
<span>15</span> <span>while (!!$row = $result->fetch_array()){//遍历全部
</span><span>16</span> <span>echo $row[1]."<br/>";
</span><span>17</span> <span>}</span><span>*/</span>
<span>18</span> 
<span>19</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>20</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>21</span> ?>

Output:

Akita Yoshi
array(6) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(9 ) "Akita Ka" ["user"]=> string(9) "Akita Ka" [2]=> string(6) "123456" ["psw"]=> string(6) "123456" }

3.1.3 Using index array fetch_row();

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>$row</span> = <span>$result</span>->fetch_row();<span>//</span><span>将结果集包装成索引数组</span>
<span>11</span> <span>echo</span> <span>$row</span>[2<span>];
</span><span>12</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>13</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>14</span> ?>

Output:

123456

3.1.4 Using associative arrays fetch_assoc();

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>$row</span> = <span>$result</span>->fetch_assoc();<span>//</span><span>将结果集包装成关联数组</span>
<span>11</span> <span>echo</span> <span>$row</span>['psw'<span>];
</span><span>12</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>13</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>14</span> ?>

Output:

123456

4 Determine the selected rows and affected rows

Usually you want to be able to determine the number of rows returned by the SELECT query, or be affected by INSERT, UPDATE or DELET queries the number of rows affected by .

We can use the num_rows and affected_rows two attributes

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>echo</span> <span>$result</span>->num_rows;<span>//</span><span>当使用查询时,想了解SELECT查询了多少行,可以使用num_rows。</span>
<span>11</span> <span>echo</span>"<br/>"<span>;
</span><span>12</span> <span>echo</span> <span>$mysqli</span>->affected_rows;<span>//</span><span>当使用查询时,想了解SELECT、INSERT、UPDATE、DELETE查询时影响的行数,可以使用affected_rows;注意,它是$mysqli下的属性</span>
<span>13</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>14</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>15</span> ?>

Output:

2
2

5 Operation of moving pointer and getting fields

  当你并不想从第一条数据开始获取,或者并不想从第一个字段获取,你可以使用数据指针移动或者字段指针移动的方式调整到恰当的位置。当然,你还可以获取字段的名称及其相关的属性

  

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span> 8</span> <span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span> 9</span> <span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>10</span> <span>echo</span> <span>$result</span>->field_count;<span>//</span><span>计算有多少条字段</span>
<span>11</span> <span>echo</span>"<br/>";<span>//</span><span>输出 3</span>
<span>12</span> <span>$field</span> = <span>$result</span>->fetch_field();<span>//</span><span>获取字段的名称</span>
<span>13</span> <span>echo</span> <span>$field</span>->name;<span>//</span><span>输出id</span>
<span>14</span> <span>echo</span> '<br/>'<span>;
</span><span>15</span> <span>while</span> (!!<span>$field</span> = <span>$result</span>->fetch_field()) {<span>//</span><span>遍历字段</span>
<span>16</span> <span>echo</span> <span>$field</span>->name.'<br />'<span>;
</span><span>17</span> <span>}
</span><span>18</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>19</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>20</span> ?>

 

 

输出:

3
id
user
psw

5.1

<?<span>php
</span><span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span>if</span>(<span>mysqli_connect_error</span><span>()){
    </span><span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
}
</span><span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码</span>
<span>$sql</span>='select * from user';<span>//</span><span>创建一句SQL语句</span>
<span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span>);<span>//</span><span>执行sql语句把结果集赋给$result</span>
<span>print_r</span>(<span>$result</span>->fetch_fields());<span>//</span><span>一次性取得字段数组</span>
<span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
?>

 

 

输出:

<span> 1</span> <span>Array</span>
<span> 2</span> <span>(
</span><span> 3</span>     [0] => stdClass <span>Object</span>
<span> 4</span> <span>        (
</span><span> 5</span>             [name] =><span> id
</span><span> 6</span>             [orgname] =><span> id
</span><span> 7</span>             [table] =><span> user
</span><span> 8</span>             [orgtable] =><span> user
</span><span> 9</span>             [def] => 
<span>10</span>             [db] =><span> test
</span><span>11</span>             [catalog] =><span> def
</span><span>12</span>             [max_length] => 1
<span>13</span>             [length] => 11
<span>14</span>             [charsetnr] => 63
<span>15</span>             [flags] => 49667
<span>16</span>             [type] => 3
<span>17</span>             [decimals] => 0
<span>18</span> <span>        )
</span><span>19</span> 
<span>20</span>     [1] => stdClass <span>Object</span>
<span>21</span> <span>        (
</span><span>22</span>             [name] =><span> user
</span><span>23</span>             [orgname] =><span> user
</span><span>24</span>             [table] =><span> user
</span><span>25</span>             [orgtable] =><span> user
</span><span>26</span>             [def] => 
<span>27</span>             [db] =><span> test
</span><span>28</span>             [catalog] =><span> def
</span><span>29</span>             [max_length] => 9
<span>30</span>             [length] => 765
<span>31</span>             [charsetnr] => 33
<span>32</span>             [flags] => 4097
<span>33</span>             [type] => 253
<span>34</span>             [decimals] => 0
<span>35</span> <span>        )
</span><span>36</span> 
<span>37</span>     [2] => stdClass <span>Object</span>
<span>38</span> <span>        (
</span><span>39</span>             [name] =><span> psw
</span><span>40</span>             [orgname] =><span> psw
</span><span>41</span>             [table] =><span> user
</span><span>42</span>             [orgtable] =><span> user
</span><span>43</span>             [def] => 
<span>44</span>             [db] =><span> test
</span><span>45</span>             [catalog] =><span> def
</span><span>46</span>             [max_length] => 6
<span>47</span>             [length] => 765
<span>48</span>             [charsetnr] => 33
<span>49</span>             [flags] => 4097
<span>50</span>             [type] => 253
<span>51</span>             [decimals] => 0
<span>52</span> <span>        )
</span><span>53</span> 
<span>54</span> )

 

5.2  

<span>1</span> <span>$result</span>->data_seek(1);<span>//</span><span>移动数据指针</span>
<span>2</span> <span>$result</span>->field_seek(1);<span>//</span><span>移动字段指针</span>

 

 6  执行多条SQL语句

  有的时候,我们需要在一张页面上同时执行多条SQL语句,之前的方法就是分别创建多个结果集然后使用。但这样资源消耗很大,也不利于管理。 PHP提供了执行多条SQL语句的方法$mysqli->multi_query();

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码
</span><span> 8</span> <span>//创建多条SQL语句</span>
<span> 9</span> <span>$sql</span> .= "SELECT * FROM user;"<span>;
</span><span>10</span> <span>$sql</span> .= "SELECT * FROM photo;"<span>;
</span><span>11</span> <span>$sql</span> .= "SELECT * FROM article"<span>;
</span><span>12</span> <span>if</span> (<span>$mysqli</span>->multi_query(<span>$sql</span>)) {<span>//</span><span>开始执行多条SQL语句</span>
<span>13</span> <span>$result</span> = <span>$mysqli</span>-><span>store_result();
</span><span>14</span> <span>print_r</span>(<span>$result</span>->fetch_array());<span>//</span><span>开始获取第一条SQL语句的结果集</span>
<span>15</span> <span>$mysqli</span>->next_result();<span>//</span><span>将结果集指针移到下一个</span>
<span>16</span> <span>$result</span> = <span>$mysqli</span>-><span>store_result();
</span><span>17</span> <span>print_r</span>(<span>$result</span>-><span>fetch_array());
</span><span>18</span> <span>$mysqli</span>-><span>next_result();
</span><span>19</span> <span>$result</span> = <span>$mysqli</span>-><span>store_result();
</span><span>20</span> <span>print_r</span>(<span>$result</span>-><span>fetch_array());
</span><span>21</span> } <span>else</span><span> {
</span><span>22</span> <span>echo</span> 'sql语句有误!'<span>;
</span><span>23</span> <span>}
</span><span>24</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>25</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>26</span> ?>

 

7  执行数据库事务

事务(transaction)是作为整个一个单元的一组有序的数据库操作。如果一组中的所有操作都成功,则认为事务成功,即使只有一个失败操作,事务也不成功。如果所有操作成功完成,事务则提交(commit),其修改将作用于所有其他数据库进程。如果一个操作失败,则事务将回滚(roll back),该事务所有操作的影响都将取消。首先,您的MySQL是InnoDB或BDB引擎的一种,一般来说,你安装了AppServ的集成包,你选择InnoDB的引擎的数据库即可。如果你建立的表不是InnoDB,可以在phpmyadmin里修改

<span> 1</span> <?<span>php
</span><span> 2</span> <span>$mysqli</span>=<span>new</span> mysqli();<span>//</span><span>实例化mysqli</span>
<span> 3</span> <span>$mysqli</span>->connect('localhost','root','admin','test'<span>);
</span><span> 4</span> <span>if</span>(<span>mysqli_connect_error</span><span>()){
</span><span> 5</span>     <span>exit</span>('数据库连接错误,错误信息是.'.<span>mysqli_connect_error</span><span>());
</span><span> 6</span> <span>}
</span><span> 7</span> <span>$mysqli</span>->set_charset("UTF8");<span>//</span><span>设置数据库编码
</span><span> 8</span> <span>//首先你必须关闭自动提交数据</span>
<span> 9</span> <span>$mysqli</span>->autocommit(<span>false</span><span>);
</span><span>10</span> <span>//</span><span>创建一个SQL语句,必须同时运行成功,不能出现一个成功,一个失败</span>
<span>11</span> <span>$sql</span> .= "UPDATE `friend` SET state=state+5 WHERE id=1;"<span>;
</span><span>12</span> <span>$sql</span> .= "UPDATE `flower` SET tg_flower=flower-5 WHERE id=1;"<span>;
</span><span>13</span> <span>//</span><span>执行两条SQL语句</span>
<span>14</span> <span>if</span> (<span>$mysqli</span>->multi_query(<span>$sql</span><span>)) {
</span><span>15</span> <span>//</span><span>获取第一条SQL一影响的行数</span>
<span>16</span> <span>$success</span> = <span>$mysqli</span>->affected_rows == 1 ? <span>true</span> : <span>false</span><span>;
</span><span>17</span> <span>//</span><span>下移,第二条SQL</span>
<span>18</span> <span>$mysqli</span>-><span>next_result();
</span><span>19</span> <span>//</span><span>获取第二条SQL影响的行数</span>
<span>20</span> <span>$success2</span> = <span>$mysqli</span>->affected_rows == 1 ? <span>true</span> : <span>false</span><span>;
</span><span>21</span> <span>//</span><span>判断是否都正常通过了,两个SQL</span>
<span>22</span> <span>if</span> (<span>$success</span> && <span>$success2</span><span>) {
</span><span>23</span> <span>$mysqli</span>-><span>commit();
</span><span>24</span> <span>echo</span> '完美提交!'<span>;
</span><span>25</span> } <span>else</span><span> {
</span><span>26</span> <span>$mysqli</span>-><span>rollback();
</span><span>27</span> <span>echo</span> '程序出现异常!'<span>;}
</span><span>28</span> } <span>else</span><span> {
</span><span>29</span> <span>echo</span> "SQL语句有误:".<span>$mysqli</span>->errno.<span>$mysqli</span>-><span>error;
</span><span>30</span> <span>}
</span><span>31</span> <span>//</span><span>最后还必须开启自动提交</span>
<span>32</span> <span>$mysqli</span>->autocommit(<span>true</span><span>);
</span><span>33</span> <span>$result</span>->free();<span>//</span><span>释放查询内存(销毁)</span>
<span>34</span> <span>$mysqli</span>->close();<span>//</span><span>别忘了关闭你的"小资源";</span>
<span>35</span> ?>

 

Statement:
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