Blogger Information
Blog 35
fans 2
comment 0
visits 22717
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli常见的增删改查--2018年4月24日
小学僧的博客
Original
1201 people have browsed it

截图如下:

20180424162259.jpg

1.数据库信息配置:mysqli_config.php

实例

<?php
define('DB_HOST','127.0.0.1');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','php');
define('DB_CHAR','utf8');

运行实例 »

点击 "运行实例" 按钮查看在线实例

2.数据库连接:mysqli_con_1.php

实例

<?php
require 'mysqli_config.php';

$db = @mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('fail');

//echo 'success!';
mysqli_set_charset($db,DB_CHAR);

运行实例 »

点击 "运行实例" 按钮查看在线实例

3.数据库查询:mysqli_query.php

实例

<?php
require 'mysqli_con_1.php';

$sql = "SELECT * FROM staff";

if ($res = mysqli_query($db,$sql)){
    while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){
        var_export($row);
        print '<hr>';
    }
} else{
    exit('query fail:'.mysqli_errno($db).':'.mysqli_connect_error($db));
}

mysqli_free_result($res);
mysqli_close($dbc);

运行实例 »

点击 "运行实例" 按钮查看在线实例

4.数据库插入:mysqli_insert.php

实例

<?php
require 'mysqli_con_1.php';

$sql = "INSERT IGNORE staff SET name='testt1',sex=0,age=20,salary=33333";

if(mysqli_query($db,$sql)){
   if(mysqli_affected_rows($db) > 0) {
       echo 'new recoder:'.mysqli_affected_rows($db).',ID is:'.mysqli_insert_id($db);
   }else{
       echo 'no new recoder';
   }
}else{
    exit(mysqli_errno($db).":".mysqli_error($db));
}

mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例

5.数据库删除:mysqli_delete.php

实例

<?php
require 'mysqli_con_1.php';

$sql = "DELETE FROM staff  WHERE staff_id=7";

if(mysqli_query($db,$sql)){
    if(mysqli_affected_rows($db) > 0) {
        echo 'delete recoder:'.mysqli_affected_rows($db);
    }else{
        echo 'no delete recoder';
    }
}else{
    exit(mysqli_errno($db).":".mysqli_error($db));
}

mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例

6.数据库更新:mysqli_update.php

实例

<?php
require 'mysqli_con_1.php';

$sql = "UPDATE staff SET salary=11122 WHERE name='testt1'";

if(mysqli_query($db,$sql)){
    if(mysqli_affected_rows($db) > 0) {
        echo 'update recoder:'.mysqli_affected_rows($db);
    }else{
        echo 'no update recoder';
    }
}else{
    exit(mysqli_errno($db).":".mysqli_error($db));
}

mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例

7.数据库多语句查询:mysqli_multi.php


实例

<?php
require 'mysqli_con_1.php';

$sql = "SELECT sex,ROUND(AVG(salary),2) FROM staff GROUP BY sex;";

$sql .= "SELECT name,age FROM staff WHERE age>30;";

$sql .= "SELECT name,salary FROM staff WHERE salary>=5000;";

$num = 1;
if(mysqli_multi_query($db,$sql)){
    echo 'num'.$num.'\'s data is:<br>';
    while($result = mysqli_store_result($db)){
        if( mysqli_num_rows($result)>0){
            while($row = mysqli_fetch_assoc($result)){
                print_r($row);echo '<br>';
            }
            mysqli_free_result($result);
        }else{
            echo 'no more data';
        }
        if(mysqli_more_results($db)){
            $num++;
            echo '<hr>num'.$num.'\'s data is:<br>';
            mysqli_next_result($db);
        }
    }
}
mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post