Simple php operations for mysql database implementation _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.