Differences between mysqli and MySQL and fetch_row and fetch_array

Source: Internet
Author: User

I. mysqli. dll is an object-based or process-based database that is easy to use. Here we will compare several common operations with MySQL. dll.

1: mysql. dll (which can be understood as a functional method ):

$ Conn = mysql_connect ('localhost', 'user ',
'Password'); // connect to the MySQL database
Mysql_select_db ('data _ base', $ conn );
// Select a database

$ Result = mysql_query ('select * from
Data_base '); // the second optional parameter specifies the connection to be opened.

$ ROW = mysql_fetch_row ($ result) // For simplicity, only one row of data is taken.
Echo $ row [0];
// Output the value of the first field

Mysqli also has a program method, but starts to use the mysqli prefix, and the others are similar. If mysqli operates in a procedural manner, some functions must specify resources, such
Mysqli_query (resource ID, SQL statement), and the resource ID parameters are placed in front, while the resource ID of mysql_query (SQL statement, 'optional ') is put
It can be left unspecified. By default, it is the last opened connection or resource.

2mysqli. dll (Object method ):

$ Conn = new mysqli ('localhost', 'user ',
'Password', 'Data _ base ');
// The connection here is new. The last parameter is to specify the database directly without mysql_select_db ().
// You can also choose not to specify it during the construction, and then
$ Conn-> select_db ('data _ base ')

$ Result = $ Conn-> query ('select * From data_base ');
$ ROW = $ result
-> Fetch_row (); // fetch a row of data
Echo row [0]; // output the value of the first field

Ii. mysql_fetch_row (), mysql_fetch_array ()

The two functions return an array. The difference is that the array returned by the first function only contains values. We can only $ row [0].
$ Row [1], which reads data by the array subscript. The array returned by mysql_fetch_array () contains both the first and key values.
In the correct format, we can read data like this (assume that the database field is
Username, passwd ):

$ Row ['username'], $ row ['passwd']

In addition, if you use ($ row as $ Kay =>
$ Value) to directly obtain the database field name.
What's more, mysqli is a new function library provided by PhP5. (I) indicates improvement and its execution speed is faster.

For example:

<? PHP

// Connect to the local MySQL database and select test as the operating database
$ Mysqli = mysqli_connect ("localhost ",
"Root", "", "test", 3306 );
// Use the mysql_query function to read data from the user table
$ Result =
Mysqli_query ($ mysqli, "select * From userinfo ");
While ($ ROW =
Mysqli_fetch_array ($ result) // read the data content cyclically.
{
?>
<Tr>
<TD
Align = "center" Height = "19"> <? PHP echo $ row ["ID"]?> </TD>
<TD
Align = "center"> <? PHP echo $ row ["name"]?> </TD>
<TD
Align = "center"> <? PHP echo
$ Row ["detail"]?> </TD>
</Tr>
<? PHP
}
// Close the connection to the database
Mysqli_free_result ($ result );
Mysqli_close ($ mysqli );*/
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.