There are three ways to connect to a database in PHP, just to discover that the connection through Mysql_connect,mysql_query has been deprecated, and now it is recommended to connect to the database through the object-oriented approach and the PDO method.
When I use the object-oriented approach to connect, the results of the query can not be displayed in Chinese, most of the solutions on the Internet are for old-fashioned connection methods, but eventually found a solution.
The general connection method is as follows:
Header("content-type:text/html; Charset=utf-8 ");$mysql _url="localhost";//the address of the database server$mysql _username="";//name of the database user name$mysql _password="";//password to connect to the database$mysql _database="";//the name of the database$db=Newmysqli ($mysql _url,$mysql _username,$mysql _password,$mysql _database);if(Mysqli_connect_error()){//prompt when database connection failsEcho' Could not connect to database. ';Exit;}Mysqli_query($db,' Set names UTF8 ');//set encoding for query results$result=$db->query ("SELECT * from Yc_brand");//hereSQLstatement surface cannot be enclosed in single or double quotes$all=$result->fetch_all ();//get all the data in the result setVar_dump($all);?>
Where the result of the query is an object $result, because it is an object, so it cannot be printed directly.
We can call the object's method to convert it to a representation such as an array.
More commonly used are fetch_row,fetch_array,fetch_all.
First, Fetch_row ()
$row = $result->fetch_row ()
The $row of this method is a one-dimensional array that only gets a set of "records" at a time, that is, if you check out 10 records, he will return only one set of records at a time.
A while loop is required to reload one-dimensional array of each query into a one-dimensional array, forming a two-dimensional array (such as a two-dimensional array with 10 sets of records).
The feature of this method is that the key name in a one-dimensional array is queried by default to a number starting from 0
$rowsarray(); // create an array to load the query results while ($result->fetch_row ()) {// to be executed as soon as the results are found $rows$row; // load the results of each check into the previously defined array }var_dump($rows);
Second, Fetch_array ()
$row = $result->fetch_array ()
This method is generally the same as fetch_row, the difference between the two is that Fetch_array () found in the one-dimensional array in each column has two key value names, the original table column name will automatically become the key value name, but also have the automatic sorting from 0 key value name
Third, Fetch_all ()
$rows = $result->fetch_all ();
This method is derived from $rows as a two-dimensional array, in fact, "is equivalent to Fetch_row method has been circulating a one-dimensional array of two-dimensional array $rows", can be printed directly.
The above describes the PHP link MySQL database, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.