This tutorial is for PHP beginners to provide a PHP connection MySQL database and query and read and write MySQL database implementation, there is a need to learn the students can refer to the next OH.
First, connect the database
Database variable file: connectvars.php
The code is as follows |
Copy Code |
Server Define (' Db_host ', ' 127.0.0.1 '); User name Define (' Db_user ', ' root '); Password Define (' Db_password ', ' root '); Database Define (' db_name ', ' test '); ?> |
Connect to a database at the point of use
The code is as follows |
Copy Code |
Require_once ' connectvars.php '; $dbc =mysqli_connect (db_host,db_user,db_password,db_name); |
Second, the database to increase, delete, change, check
Query, modify, delete operations are written as SQL statements
The code is as follows |
Copy Code |
$query = "SELECT * from Toyota ORDER by ID DESC"; $data save information is rich, can be used to determine whether the operation is successful, or use it to fetch data, query the number of records $data = Mysqli_query ($dbc, $query); |
Third, determine whether the operation is successful
The code is as follows |
Copy Code |
if ($data) { Echo ' data operation succeeded '; }else{ Echo ' data operation failed '; } |
Iv. number of records obtained for enquiry
The code is as follows |
Copy Code |
$count = Mysqli_num_rows ($data); Echo $count; |
V. to cycle through the records
Suppose a field in the record is ' URL '
The code is as follows |
Copy Code |
while ($row = Mysqli_fetch_array ($data)) { echo $row [' url ']; } |
http://www.bkjia.com/PHPjc/632943.html www.bkjia.com true http://www.bkjia.com/PHPjc/632943.html techarticle This tutorial is for PHP beginners to provide a PHP connection MySQL database and query and read and write MySQL database implementation, there is a need to learn the students can refer to the next OH. First, the connection data ...