The difference between Mysqli_connect and mysql_connect
Ladies and gentlemen Brother Siang, who has time to tell me in detail the difference between mysqli and MySQL AH. I am a novice, looking confused.
I use MYSQLI query database output when there is garbled, do not know how to solve. See someone else write code, if using MySQL can use mysql_query ("Set names ' GBK '") to change the code, if directly changed to Mysqli_query ("Set names ' GBK '") will error, helpless.
------Solution--------------------
Mysqli_set_charset (' Connect database ', ' You set the Code ');
------Solution--------------------
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 to 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, and 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, and 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