This article describes php Data Access in detail through examples. There are two methods for data access, interested friends can refer to the examples in this article to share two types
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.