: This article mainly introduces the basic knowledge of Mysqli. For more information about PHP tutorials, see. I believe that when I first started learning php, MySQL is the preferred choice for databases and mysql extension is the preferred choice for database connection expansion. However, with the improvement of php version, mysql extensions are gradually replaced by mysqli and PDO. As described in deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead. Learning mysqli extension is imperative.
Compared with mysql extension, mysqli extension supports object-oriented and process-oriented methods, pre-processing, transaction processing, and faster than mysql. This article describes the basic and simple operations of object-oriented mysqli.
Install and configure mysqli
The installation configuration of mysqli is the same as that of other configurations. First, make sure that the php_mysqli.dll file exists in your ext folder (in general, it exists), and in php. remove the line ";" before "extension = php_mysqli.dll" in the INI file and confirm the extensi/span>
How can I verify that mysqli extension is enabled?
In fact, the most direct thing is to use the mysqli extension function to see if it can be used. for example, you can determine whether the extension has been installed by connecting to the database. If the connection is successful, you don't have to say that it is installed. if the connection is not successful, do not think that it is not installed. we still have some tips to use the phpinfo () function, we can clearly know whether mysqli is available.
Of course, you can use extension_loaded ('mysqli') to determine whether to load the mysqli extension. you can even use get_loaded_extensions () to determine which extensions are loaded.
Use of object-oriented mysqli
For developers who have used mysql extensions, mysqli is easy to understand whether it is object-oriented or process-oriented. For details about the attributes, refer to the php official manual at http://php.net/manual/zh/mysqli.summary.php. For more information, see the usage process of the signature code example mysqli.
In this example, the operation table is a test table with two fields: id and title.
Connect_errno) {"Connect Error :". $ mysqli-> connect_error;} // set the default character set $ mysqli-> set_charset ('utf8'); $ SQL = "select * from test "; // Generate the mysql_result object $ result = $ mysqli-> query ($ SQL); // returns a two-dimensional associated array. Similarly, the parameter can be set to MYSQLI_NUM to return the index array or MYSQLI_BOTH. $ Rows = $ result-> fetch_all (MYSQLI_ASSOC); // Adjust the result pointer to any row $ result-> data_seek (2 ); $ row = $ result-> fetch_row (); // $ row = $ result-> fetch_array (); // $ row = $ result-> fetch_assoc (); // $ row = $ result-> fetch_object (); // release the result set $ result-> free (); // $ result-> free_result (); // $ result-> close (); // close the connection $ mysqli-> close ();
The above code simply shows how to use mysqli for queries without traversing the query result set. it is not difficult to retrieve the data in the array.
Note that the SQL statements executed by $ mysqli-> query () are successfully executed.SELECT, SHOW, DESCRIBEOrEXPLAINA mysqli_result object is returned for the query.TRUE. if the execution fails, false is returned.
You can call $ mysqli-> affected_rows to obtain the number of affected records during INSERT, UPDATE, and DELETE operations.
$ Mysqli-> affected_rows returned value-1 indicates that the SQL statement has a problem, 0 indicates that no affected records are returned, and other values indicate the number of affected records.
Executing multiple SQL statements, preprocessing, and transaction processing are also important to mysqli. I will write them in later articles.
P s: I wrote a technical blog for the first time! I hope you can give me more comments if you are on an improper call ~
The above introduces the basic knowledge of Mysqli, including some content, and hopes to help those who are interested in the PHP Tutorial.