The difference between Mysqli_connect and mysql_connect

Source: Internet
Author: User
Tags sql error

One: Mysqli.dll is a database that allows the operation of objects in a way or process, and it is easy to use. Here is a comparison of a few common operations and mysql.dll.
1:mysql.dll (can be understood as a functional approach):

$conn = mysql_connect (' localhost ', ' user ', ' password ');//Connect MySQL database
mysql_select_db (' data_base ');//Select Database
  
$result = mysql_query (' select * from Data_base ');//There is a second optional parameter that specifies the open connection

$row = mysql_fetch_row ($result))//For simplicity, only one row of data is taken here
echo $row [0];//output The value of the first field

Mysqli also have a procedural approach, but start with the mysqli prefix, the others are similar. If Mysqli is manipulated in a procedural way, some functions must specify a resource, such as mysqli_query (resource ID, SQL statement), and the resource identification parameter is placed in front, while the mysql_query (SQL statement, ' optional ') resource identifier is placed in the back , and can not be specified, it is the last open connection or resource by default.

2mysqli.dll (object mode):

$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 ()
//can also be constructed when not specified, then $conn-select_db (' data_base ')

$result = Query (' SELECT * from Data_base '), $conn
$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 (   )

These two functions return an array, the difference is that the first function returns an array that contains only the values, we can only $row[0],
$row [1], which reads the data as an array subscript, and the array returned by Mysql_fetch_array () contains both the first and key values
In the right form, we can read the data in this way (if the database field is USERNAME,PASSWD):

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

Furthermore, if you use ($row as $kay + $value), you also get the field name of the database directly.
more important is that mysqli is a new library of functions provided by PHP5, (i) for improvement, and for faster execution.
-----------------------------------------------------------------------------

your SQL error is because you call mysqli the wrong way
Reference:
$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 ()
//can also be constructed when not specified, then $conn-select_db (' data_base ')

$result = Query (' SELECT * from Data_base '), $conn

The difference between Mysqli_connect and mysql_connect

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.