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
The code is as follows
|
Copy code
代码如下 |
复制代码 |
while($row = mysqli_fetch_array($data)){
echo $row['url'];
}
|
|
|
if($data){
echo 'Data operation successful';
}else{
echo 'Data operation failed';
}
4. Obtain the number of query records
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...
|
|
|
|