zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 PHP开发学习:MySQL 插入多条数据

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $dbname = "myDB";
    
    // 创建链接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // 检查链接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Mary', 'Moe', 'mary@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Julie', 'Dooley', 'julie@example.com')";
    
    if ($conn->multi_query($sql) === TRUE) {
        echo "新记录插入成功";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    $conn->close();
    ?>

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
     
    // 创建链接
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // 检查链接
    if (!$conn) {
        die("连接失败: " . mysqli_connect_error());
    }
     
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Mary', 'Moe', 'mary@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Julie', 'Dooley', 'julie@example.com')";
     
    if (mysqli_multi_query($conn, $sql)) {
        echo "新记录插入成功";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
     
    mysqli_close($conn);
    ?>
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDBPDO";
     
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     
        // 开始事务
        $conn->beginTransaction();
        // SQL 语句
        $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
        VALUES ('John', 'Doe', 'john@example.com')");
        $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
        VALUES ('Mary', 'Moe', 'mary@example.com')");
        $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
        VALUES ('Julie', 'Dooley', 'julie@example.com')");
     
        // 提交事务
        $conn->commit();
        echo "新记录插入成功";
    }
    catch(PDOException $e)
    {
        // 如果执行失败回滚
        $conn->rollback();
        echo $sql . "<br>" . $e->getMessage();
    }
     
    $conn = null;
    ?>
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
     
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    } else {
        $sql = "INSERT INTO MyGuests(firstname, lastname, email)  VALUES(?, ?, ?)";
     
        // 为 mysqli_stmt_prepare() 初始化 statement 对象
        $stmt = mysqli_stmt_init($conn);
     
        //预处理语句
        if (mysqli_stmt_prepare($stmt, $sql)) {
            // 绑定参数
            mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
     
            // 设置参数并执行
            $firstname = 'John';
            $lastname = 'Doe';
            $email = 'john@example.com';
            mysqli_stmt_execute($stmt);
     
            $firstname = 'Mary';
            $lastname = 'Moe';
            $email = 'mary@example.com';
            mysqli_stmt_execute($stmt);
     
            $firstname = 'Julie';
            $lastname = 'Dooley';
            $email = 'julie@example.com';
            mysqli_stmt_execute($stmt);
        }
    }
    ?>
  • 相关阅读:
    windows phone 8 开发系列(三)程序清单说明与配置
    windows phone 8 开发系列(二)Hello Wp8!
    windows phone 8 开发系列(一)环境搭建
    MVC3 ViewBage 输出的值 被编码
    汉字取首字母拼音 ---vue---js
    一个基于POI的通用excel导入导出工具类的简单实现及使用方法
    MongoDB基础命令笔记
    Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题
    jQuery与ajax 基础运用
    mysql常处理用时间sql语句
  • 原文地址:https://www.cnblogs.com/tszr/p/10955572.html
Copyright © 2011-2022 走看看