Learning Essentials
- PHP Access MySQL Configuration
- PHP Access MySQL function introduction
- Football Schedule Information Management
PHP access MySQL configuration php.ini configuration file confirm that the following configuration is turned on
Extension=php_mysql.dll PHP5 before accessing the database module
Extension=php_mysqli.dll PHP5 after accessing the database module
Get support information via GetInfo () to verify that PHP has supported MySQL access
PHP Access MySQL function introduction Mysqli_connect () function
Open a new connection to the MySQL server.
Mysqli_connect (Host,username,password,dbname,port,socket);
Parameters |
Describe |
Host |
Optional. Specifies the host name or IP address. |
Username |
Optional. Specifies the MySQL user name. |
Password |
Optional. Specify the MySQL password. |
dbname |
Optional. Specifies the database that is used by default. |
Port |
Optional. Specifies the port number to attempt to connect to the MySQL server. |
Socket |
Optional. Specify the socket or named pipe to be used. |
Returns an object that represents the connection to the MySQL server, the resource type.
$link =mysqli_connect (' localhost',' root ',' rootkit ' ,' MySchool ');
Description: MySQL default port 3306, the host address can omit the port number, or omit the port number parameter. So it can be written in the following form.
Mysqli_query () function
Executes a query against the database.
Mysqli_query (Connection,query,resultmode);
Parameters |
Describe |
Connection |
Necessary. Specifies the MySQL connection to use. |
Query |
Required, specifies the query string. |
Resultmode |
Optional. A constant. Can be any one of the following values: Ümysqli_use_result (Use this if you need to retrieve large amounts of data) Ümysqli_store_result (default) |
A Mysqli_result object is returned for a successful SELECT, SHOW, DESCRIBE, or EXPLAIN query. Returns TRUE for other successful queries. If it fails, it returns FALSE.
Special Note : The function of asynchronous query is added in PHP 5.3.0.
Build the SQL statement string $query= "SELECT * from student";//execute the SQL statement and return the array result set $result=mysqli_query ($link, $query);
Mysqli_num_rows () function
Returns the number of rows in the result set.
mysqli_num_rows (result);
Parameters |
Describe |
Result |
Necessary. Specifies the result set identifier returned by Mysqli_query (), Mysqli_store_result (), or Mysqli_use_result (). |
Returns the number of rows in the result set.
Gets the number of records $rownum=mysqli_num_rows ($result);
MYSQLI_FETCH_ASSOC () function
Fetch a result row as an associative array.
Array Mysqli_fetch_assoc (Mysqli_result $result);
Parameters |
Describe |
Result |
Procedural style only:a result set identifier returned by Mysqli_query (), Mysqli_store_result () or Mysqli_use_result (). |
Returns an associative array of strings representing the fetched row in the result set, where each key in the array repres Ents the name of the one of the result set ' s columns or NULL if there is no more than rows in resultset.
If or more columns of the result has the same field names, the last column would take precedence. To access the other column (s) of the same name, either need to access the result with numeric indices by using Mysqli_ Fetch_row () or add alias names.
Returns a row of records from the array result set, the return value is an associative array structure//mysqli_fetch_object ($result) returns an object $row=mysqli_fetch_assoc ($result); Echo $row [' Studentno ': ';//The element value is determined by the database field name echo $row [' Studentname ']. " <br> "; ;
Mysqli_free_result () function
Releases the resulting result set memory.
Mysqli_free_result (Result)
Parameters |
Describe |
Result |
Necessary. Specifies the result set identifier returned by Mysqli_query (), Mysqli_store_result (), or Mysqli_use_result (). |
no return value.
Mysqli_close () function
Close the previously opened database connection.
Mysqli_close (connection);
Parameters |
Describe |
Connection |
Specifies the MySQL connection to be closed. |
Returns TRUE if successful, or FALSE if it fails.
Complete database access steps
- Establishing a data connection
- Building SQL statements
- Get result set
- Working with result sets
- Close the result set resource
- To close a database connection
Complete sample Code
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "SELECT * from Teaminfo";//3, query $result = mysqli_query ($link, $query);//4, processing result set $rownum = Mysqli_n Um_rows ($result); Gets the number of rows for the result set for ($i = 0; $i < $rownum; $i + +) {$row = Mysqli_fetch_assoc ($result); echo $row [' ID ']. ' = = = '. $row [' Teamname ']. ' <br> ';} 5, release the result set Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "INSERT into Teaminfo (teamname) VALUES (' Xiamen International Trade ')";//3, query $result = mysqli_query ($link, $query);//4, Process result set if ($result) {echo ' Add team information successfully! ‘;} else {echo ' added team info failed! '; Echo mysqli_error ($link); Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "UPDATE teaminfo SET teamname= ' xiamen Seven Wolves ' WHERE id=19";//3, query $result = mysqli_query ($link, $query);// 4. Process result set if ($result) {echo ' Update team information successfully! ‘;} else {echo ' update team info failed! '; Echo mysqli_error ($link);} 5. Release result set//Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "DELETE from Teaminfo WHERE id=17";//3, query $result = mysqli_query ($link, $query);//4, processing result set if ($res ult) {echo ' Delete team information successfully! ‘;} else {echo ' deleted team info failed! '; Echo mysqli_error ($link);} 5. Release result set//Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);
On-Machine exercise: 2018 FIFA World Cup Asia regional schedule information management
using mysqli implement the following use cases:
- Achieve landing
- Implement Add schedule information
- Implement Delete schedule information
- Implement Modify schedule information
- Pagination Display for schedule information
PHP16 PHP Access MySQL