There are two main ways in which PHP connects to MySQL database:
1. Process-oriented approach: not recommended
// 1. Obsolete methods /* //Generate connection $db _connect=mysql_connect ("localhost", "root", "324528"); Select the Operational database mysql_select_db ("Testa", "$db _connect"); Write SQL statement $sql = "SELECT * from Info"; Execute SQL statement $result =mysql_query ($sql); Processing Queries $row =mysql_fetch_row ($result); Print_r ($row); */
2. The object-oriented approach is the most common way to connect:
This method of PHP connection to the database is mainly divided into: (1) Create a connection (2) Determine whether the connection succeeds (3) write the SQL statement (4) Execute the SQL statement (5) to process the result
1. Create the connection $db= new mysqli ("localhost", "root", "324528", "Testa"),//2. Determine if the connection was successful!mysqli_connect_error () or Die ("Connection Failed");// 3. Write SQL statements $sql= "SELECT * from Info";//4. Execute SQL statement $result= $db->query ($sql);//5. Operation result Data//(1) Fetch_row ()//$row =$ Result->fetch_row ();/*while ($row = $result->fetch_row ()) {Print_r ($row);} *///(2) Fetch_all () returns all data in the form of a two-dimensional array/* $attr = $result->fetch_all ();p rint_r ($attr); *///(3) Fetch_assoc (); Returns a piece of data, in the form of an associative array,/* $attr = $result->fetch_assoc ();p rint_r ($attr); */
(3.24) How PHP connects data