Simple php to operate mysql database implementation. This tutorial provides php beginners with an article about how to connect php to the mysql database and query and read/write the mysql database. if you need to learn, refer to the tutorial. 1. connection data this tutorial provides php beginners with an article on how to connect php to mysql database and query and read/write mysql database. if you need to learn, refer to the workshop.
1. connect to the database
Database variable File: connectvars. php
The code is as follows: |
|
// Server Define ('Db _ host', '2017. 0.0.1 '); // User name Define ('Db _ user', 'root '); // Password Define ('Db _ password', 'root '); // Database Define ('Db _ name', 'test '); ?> |
Connect to the database in use
The code is as follows: |
|
Require_once 'connectvars. php '; $ Dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME ); |
2. add, delete, modify, and query databases
// Query, modify, and delete operations are all written as SQL statements.
The code is as follows: |
|
$ Query = "SELECT * FROM toyota order by id desc "; // $ Data stores a wealth of information and can be used to determine whether the operation is successful or use it to retrieve data and query the number of records $ Data = mysqli_query ($ dbc, $ query ); |
III. determine whether the operation is successful
The code is as follows: |
|
If ($ data ){ Echo 'data operation successful '; } Else { Echo 'data operation failed '; } |
4. obtain the number of query records
The code is as follows: |
|
$ Count = mysqli_num_rows ($ data ); Echo $ count; |
5. perform cyclic operations on records
Assume that there is a field in the record as 'URL'
The code is as follows: |
|
While ($ row = mysqli_fetch_array ($ data )){ Echo $ row ['URL']; } |
Bytes. 1. connect data...