Core PHP Programming --Mysql extension

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yangmolulu/article/details/91468945

Mysql extension

table of Contents

Mysql extension

Even the basic operation of the library

Connect to the database server

Provided to connect the encoder

Select the database you want to use

Close the connection

CRUD operations performed

Execute the query operation

Execute a query

Gets the number of rows in the result set

Analytical results set

Other related functions

Information about the field

Related error messages

Other functions


PHP extensions for mysql database operations provided: Allow PHP mysql as a client connection to the server to operate.

 

Even the basic operation of the library

Connect to the database server

1) mysqli_connect (server address, user name, password)

(In php7 abolished mysql_connect (), instead mysqli_connect ())

 

Provided to connect the encoder

To maintain smooth communication between the client and the server: the same "language" (character sets)

1) in the form of 1: mysqli_query ($ link, "set names XXX");

 

2) in the form of 2: mysqli_set_charset ( "XXX")

 

3) how to determine what encoding to use?

The client interface is currently executing script what character set, it is set to any character set

 

 

Select the database you want to use

1) in the form of 1: mysqli_query ($ link, "use XXX");

2) forms 2: mysqli_select_db ($ link, "XXX");

 

Close the connection

Take the initiative to release the connection: connection mysql server resources are limited, without a need to release (the end of the script execution system will automatically release)

1)mysqli_close($link);

 

 

CRUD operations performed

CRUD operations performed

1) mysqli_query ($link, “insert…”);

2) mysqli_query ($link, “delete…”);

3) mysqli_query ($link, “update…”);

 

Script does a dedicated database initialization: After the operation of the database that contains the script to

 

1, the insert

 

2, update

 

 

Judgment execution results

 

1) is successful, the result is true: represent SQL commands executed successfully, the database does not mean a change

2) fails, the result is false: the representative SQL commands or erroneous operation object does not exist

 

Execute the query operation

Execute a query

1)mysqli_query($link,”select…”)

2)其他类似查询语句,比如:show语句,desc语句

以上两种情况的总结:凡是执行操作希望拿到数据库返回的数据进行展示的(结果返回:数据结果)

 

3)执行结果的处理:成功为结果集,失败为false

成功返回结果:SQL指令没有错误,但是如果 查询结果本身为空返回也是true(结果集是一种资源:转换成bool永远为true

失败为false:SQL指令有错误

获取结果集行数

1)mysqli_num_rows():获取结果集中到底有多少行

 

 

 

 

解析结果集

按照结果集中所在位置取出对应的一条记录(一行),返回一个数组,同时指针下移…直到指针溢出结果集。

1)mysqli_fetch_assoc():获取关联数组,表单字段名字作为数组下标,元素值作为数组元素值。

 

2)mysqli_fetch_row():获取索引数组,只获取数据的值,然后数组的下标从0开始,自动索引

 

3)mysqli_fetch_array():获取关联或者索引数组,但是默认同时存在:一个记录取两次,形成一组是关联数组,一组是索引数组,但是可以通过第二个参数决定获取的方式:MYSQLI_ASSOC只获取关联数组, MYSQLI_NUM只获取索引数组,MYSQLI_BOTH默认值,获取全部。这种效率较低,不建议使用。

 

 

通常结果集的操作:一般是获取里面的所有(全部记录)

fetch_all()

抓取所有的结果行并且以关联数据,数值索引数组,或者两者皆有的方式返回结果集。

fetch_array()

以一个关联数组,数值索引数组,或者两者皆有的方式抓取一行结果。

fetch_object()

以对象返回结果集的当前行。

fetch_row()

以枚举数组方式返回一行结果

fetch_assoc()

以一个关联数组方式抓取一行结果。

fetch_field_direct()

以对象返回结果集中单字段的元数据。

fetch_field()

以对象返回结果集中的列信息。

fetch_fields()

以对象数组返回代表结果集中的列信息。

 

                                                               fetch_all (从结果集中取得所有行)


                              fetch_array (以一个关联数组,数值索引数组,或者两者皆有的方式抓取一行结果)


                                                            fetch_object (以对象返回结果集的当前行)      

                                                       fetch_row (以枚举数组方式返回一行结果)


                                                     fetch_assoc (以一个关联数组方式抓取一行结果)

 

                                            fetch_field_direct (以对象返回结果集中单字段的元数据既单列的信息)

                                                            fetch_field (以对象返回结果集中的列信息)

 


                                                          fetch_fields (以对象数组返回代表结果集中的列信息)

其他相关函数

有关字段信息

1)mysqli_num_fields():获取一个指定结果集中所有的字段数, 返回结果集中的字段数(列数)。

 

2)mysqli_num_rows() 返回结果集中的行数

3)  mysqli_fetch_lengths()  返回结果集中当前行的列长度

有关出错信息

1)mysqli_error($link):获取出错对应的提示信息

2)mysqli_errno($link):获取出错对应的错误提示代号

 

错误的判断:基于mysqli_query()这个函数执行的结果,结果返回false就代表执行错误

 

其他函数

1) mysqli_insert_id($link):返回最后一条插入语句产生的自增ID,如果没有自增长ID返回0

 

 

 

2) mysql_free_result($result);        //释放查询的结果集资源

Guess you like

Origin blog.csdn.net/yangmolulu/article/details/91468945