Home > Backend Development > PHP Tutorial > Simple PHP operation mysql database implementation_PHP tutorial

Simple PHP operation mysql database implementation_PHP tutorial

WBOY
Release: 2016-07-13 10:46:38
Original
779 people have browsed it

This tutorial provides PHP beginners with an article about the implementation of PHP connecting to mysql database and querying, reading and writing mysql database. Students who need to learn can refer to it.

1. Connect to database

Database variable file: connectvars.php

 代码如下 复制代码
//服务器
define('DB_HOST', '127.0.0.1');
//用户名
define('DB_USER', 'root');
//密码
define('DB_PASSWORD', 'root');
//数据库
define('DB_NAME','test') ;
?>

Connect to the database at the point of use

The code is as follows
 代码如下 复制代码

require_once 'connectvars.php';
$dbc =mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

Copy code

require_once 'connectvars.php';
$dbc =mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

 代码如下 复制代码
$query = "SELECT * FROM toyota ORDER BY ID DESC";
//$data保存信息较为丰富,可用其判断是否操作成功,或利用其取数据,查询记录条数
$data = mysqli_query($dbc,$query);

 代码如下 复制代码

if($data){
     echo '数据操作成功';
}else{
     echo '数据操作失败';
}

2. Add, delete, modify and check the database

//Queries, modifications, and deletions are all written as SQL statements
 代码如下 复制代码

$count = mysqli_num_rows($data);
echo $count;

3. Determine whether the operation is successful

if($data){
echo 'Data operation successful';
}else{
echo 'Data operation failed';
}
The code is as follows Copy code
 代码如下 复制代码

while($row = mysqli_fetch_array($data)){
     echo $row['url'];
}

The code is as follows Copy code
$count = mysqli_num_rows($data);
echo $count; 5. Loop the records Suppose there is a field in the record as 'url'
The code is as follows Copy code
while($row = mysqli_fetch_array($data)){
echo $row['url'];
} http://www.bkjia.com/PHPjc/632943.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632943.htmlTechArticleThis tutorial provides an article for PHP beginners about the implementation of PHP connecting to mysql database and querying, reading and writing mysql database. , students who need to learn can refer to it. 1. Connect data...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template