Blogger Information
Blog 64
fans 2
comment 3
visits 75698
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql增删改查
清雨的博客
Original
661 people have browsed it


mysqli_config.php

实例

<?php
/**
 * 配置数据库连接参数
 */
define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASS', 'root');
define ('DB_NAME', 'php');
define ('DB_CHAR', 'utf8');

运行实例 »

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

mysqli_connect.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:12
 */

/*
*创建连接,返回连接资源
*判断连接成功
*选择数据表
*设置字符集
*/

//导入数据库连接参数
require 'mysqli_config.php';

//2. 调用连接函数,成功则返回mysqli对象,失败返回false
$db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("连接失败");


//echo '<h1>连接成功</h1>';


//5. 设置默认字符集
mysqli_set_charset($db, DB_CHAR);

运行实例 »

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

mysqli_delete.php

实例

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/24
* Time: 9:17
*/
require 'mysqli_connect.php';

$sql="DELETE FROM staff WHERE name='灭绝师太';";

if (mysqli_query($db,$sql)) {
   if (mysqli_affected_rows($db)>0) {
       echo '成功删除了'.mysqli_affected_rows($db).'条记录';
   } else {
       echo '没有记录被删除';
   }
} else {
   exit(mysqli_errno($db).':'.mysqli_error($db));
}

mysqli_close($db);


运行实例 »

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

mysqli_insert.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:18
 */
require 'mysqli_connect.php';

$sql="INSERT INTO staff (name,sex,age,salary) VALUES ('梅超风',1,30,6000);";

if (mysqli_query($db,$sql)) {
    if (mysqli_affected_rows($db)>0) {
        echo "新增了".mysqli_affected_rows($db)."条记录,<br>新纪录主键id:"
            .mysqli_insert_id($db);
    } else {
        echo "没有记录被新增";
    }
} else {
    exit(mysqli_errno($db).':'.mysqli_error($db));
}

mysqli_close($db);

运行实例 »

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

mysqli_multiquery.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:21
 */
require 'mysqli_connect.php';

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

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

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

$num=1;

if (mysqli_multi_query($db,$sql)) {
    echo $num.'结果子集<br>';
    // 获取当前子结果集
    while ($res=mysqli_store_result($db)) {
        if (mysqli_affected_rows($db)>0) {
            while ($row=mysqli_fetch_array($res,MYSQLI_ASSOC)) {
                print_r($row);
                echo "<br>";
            }

            mysqli_free_result($res);
        } else {
            echo '没有满足结果的数据';
        }

        if (mysqli_more_results($db)) {
            $num++;
            echo '<hr>第'.$num.'个结果子集的数据<br>';
            mysqli_next_result($db);
        }
    }
} else {
    exit(mysqli_errno($db).':'.mysqli_error($db));
}

mysqli_close($db);

运行实例 »

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

mysqli_query.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:16
 */
require 'mysqli_connect.php';

$sql="SELECT name,age FROM staff;";

if ($res=mysqli_query($db,$sql)) {
    if (mysqli_num_rows($res)>0) {
        while ($row=mysqli_fetch_assoc($res)) {
            var_export(print_r($row),true);
            echo "<hr>";
        }
    }
} else {
    exit(mysqli_errno($db).':'.mysqli_error($db));
}

// 释放结果集,只对select
mysqli_free_result($res);

mysqli_close($db);

运行实例 »

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

mysqli_update.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:20
 */
require 'mysqli_connect.php';

$sql="UPDATE staff SET salary=7000 WHERE name='杨过';";

if (mysqli_query($db,$sql)){
    if(mysqli_affected_rows($db)>0) {
        echo '成功更新了'.mysqli_affected_rows($db).'条记录';
    } else {
        echo '没有更新';
    }
} else {
    exit(mysqli_errno($db).':'.mysqli_error($db));
}

mysqli_close($db);

运行实例 »

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

效果图

1.png

总结:

在网站开发过程中,我们是绝对实用MySQL的,因为需要进行数据存储等,在网站存储数据是用到增删改查操作是必须要学习知识。

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