Differences between mysqli_connect and mysql_connect

Source: Internet
Author: User
What is the difference between mysqli_connect and mysql_connect? what is the difference between mysqli and mysql. I am a newbie and look confused. I used mysqli to query the database output with garbled characters. I don't know how to solve this problem. Check the code written by others. if mysql is used, use mysql_query (set & nbsp; names & nbsp; GBK) & nbsp; to change the differences between mysqli_connect and mysql_connect.
Which of the following has time to tell me the difference between mysqli and mysql. I am a newbie and look confused.
I used mysqli to query the database output with garbled characters. I don't know how to solve this problem. Check the code written by others. if mysql is used, use mysql_query ("set names 'gbk'") to change the encoding. if it is directly changed to mysqli_query ("set names 'gbk '") an error is reported.


------ Solution --------------------
Mysqli_set_charset ('Connect to database', 'Encoding you Set ');
------ Solution --------------------
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'); // select a database
  
$ Result = mysql_query ('select * from data_base '); // There is a second optional parameter, specifying the opened connection.

$ 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, for example, mysqli_query (resource ID, SQL statement), and the parameters of the resource ID are placed in the front, the resource ID of mysql_query (SQL statement, 'option') is placed behind and can be left unspecified. it is the last opened connection or resource by default.

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 this parameter during construction, and then $ conn-> select_db ('data _ base ')

$ Result = $ conn-> query ('select * from data_base ');
$ Row = $ result-> fetch_row (); // retrieves 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 (if the database field is username, passwd ):

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

In addition, if you use ($ row as $ kay => $ value) for operations, you can 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.
-----------------------------------------

Your SQL error is caused by the incorrect method of calling mysqli.
Refer:
$ 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 this parameter during construction, and then $ conn-> select_db ('data _ base ')

$ Result = $ conn-> query ('select * from data_base ');

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.