Simple PHP operation MySQL database implementation _php Tutorial

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

  • 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.