The instance explains php Data access, and the instance explains php Data. The example explains php Data Access and the example explains php Data. the example in this article shares two php Data access methods. you can compare and analyze the similarities and differences between the two methods, finally, we will explain php Data access for large instances and php Data for example.
This article provides two examples:Php Data access methodYou can make a comparison, analyze the similarities and differences between the two methods, and finally provide you with a small exercise. the specific content is as follows:
Method 1:Out of date, only understand
1. create a connection (establish a channel)
$ Db = mysql_connect ("localhost", "root", "123"); // The server address, user name, and password are in brackets"
2. select the database to operate
Mysql_select_db ("mydb", "$ db ");
3. write SQL statements
$ SQL = "select * from Info ";
4. execute SQL statements
$ Result = mysql_query ($ SQL); // query indicates a query.
5. retrieve data from the result set ($ result)
$ Row = mysql_fetch_row ($ result); // read a row of data every time you execute $ row1 = mysql_fentch_row ($ result); // execute the second row of data var_dump ($ row ); // read all data cyclically: while ($ row = mysql_fetch_row ($ result) {var_dump ($ row );}
Method 2:Object-oriented
1. create a connection object:
$ Db = new MySQLi ("localhost", "root", "123", "mydb") // The content in brackets is "server address", "user name ", "password" and "database name"
2. identify connection errors:
2.1 mysqli_connect_error (); // indicates a connection error.
2.2
If (mysqli_connect_erroe ())
{
Echo "connection failed! ";
Exit (); // exit the program
}
2.3! Mysqli_connect_error or die ("connection failed! "); //" Or "indicates that the connection is correct, and" or "indicates that the connection fails.
3. write an SQL statement:
$ SQL = "select * from nation ";
4. run the SQL statement: if the execution is successful, return the result set Object. if the execution fails, return false.
$ Result = $ db-> query ($ SQL );
5. read data from the result set and determine whether data exists first
If ($ result) {// returns the index array of a row of data. each execution returns a data var_dump ($ result-> fetch_row ()); while ($ row = $ result-> fetch_row) {var_dump ($ row);} // returns the associated array of a row of data, each execution returns a data var_dump ($ result-> fetch_row (); // returns all data var_dump ($ result-> fetch_all () through a two-dimensional array ()); // returns a row of data as an object var_dump ($ result-> fetch_object ());}
Exercise:
1. The nation table is displayed on the page as follows:
$ Db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connection_erroe () or die ("connection failed! "); $ SQL =" select * from nation "; $ result = $ db-> query ($ SQL); if ($ result) {$ att = $ result-> fetch_all (); echo""; Foreach ($ att as $ value) {echo"{$ Value [1]}";} Echo"";}
2. check the Info table and display it as a table.
$ Db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connecton_error () or die ("connection failed! "); $ SQL =" select * from info "; $ result = $ bd-> query ($ SQL); if ($ result) {$ att = $ result-> fetch_all (); echo"
"; Echo"
| Code |
Name |
Gender |
Ethnicity |
Birthday |
"; Foreach ($ att as $ value) {echo"
| {$ Value [0]} |
{$ Value [1]} |
{$ Value [2]} |
{$ Value [3]} |
{$ Value [4]} |
";} Echo"
";}// You can also use the for loop if ($ result) {$ arr = $ result-> fetch_all (); echo"
"; Echo"
| Code |
Name |
Sex |
Nation |
Birthday |
"; For ($ I = 0; $ I
| {$ Arr [$ I] [0]} |
{$ Arr [$ I] [1]} |
{$ Arr [$ I] [2]} |
{$ Arr [$ I] [3]} |
{$ Arr [$ I] [4]} |
";} Echo"
";}
The above is all the content of this article. I hope it will help you learn php programming.
Examples in this article share two php Data access methods. you can compare and analyze the similarities and differences between the two methods...