zoukankan      html  css  js  c++  java
  • php mysqli 预处理操作数据库 _fei

     用到的SQL表

    CREATE TABLE `student_01` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '名字',
      `kecheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
      `score` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
      `other_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`) USING BTREE,
      KEY `aaaa` (`other_id`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
    
    输入变量的数据处理
    //输入变量的数据处理
    //输入变量的过程如下:
    // 01) 预备(解析)语句  02) 绑定变量 03) 赋值到绑定变量 04) 执行预备语句
    $conn = mysqli_connect('127.0.0.1', 'afei2', '123456', 'test');
    $conn->query("set names utf8mb4");
    $stmt = $conn->prepare("INSERT INTO student_01(name, kecheng, score,other_id) VALUES (?, ?, ?, ?)");
    $stmt->bind_param('ssdi',$name, $kecheng, $score,$other_id);//第一个参数是指定类型
    
    $name = '大飞';
    $kecheng = '数学';
    $score = 75;
    $other_id = 1;
    $stmt->execute();
    
    $name = '大飞02';
    $kecheng = '语文';
    $score = 60;
    $other_id = 1;
    $stmt->execute();
    
    $stmt->close();
    
    绑定变量获取的例子
    //绑定变量获取的例子
    //输出变量的过程如下:
    // 01) 预备(解析)语句   02) 执行预备语句   03) 绑定输出变量  04) 把数据提取到输出变量中
    $conn = mysqli_connect('127.0.0.1', 'afei2', '123456', 'test');
    $conn->query("set names utf8mb4");
    $stmt = $conn->prepare("SELECT id,name,kecheng,score FROM student_01");
    $stmt->bind_result($id, $name,$kecheng,$score);//这里定义的变量
    $stmt->execute();
    print "<table  border='1' >" . PHP_EOL;
    print "<tr><th>ID</th><th>姓名</th><th>课程</th><th>分数</th></tr>" . PHP_EOL;
    while ($stmt->fetch()) {
        print "<tr><td>$id</td><td>$name</td><td>$kecheng</td><td>$score</td></tr>" . PHP_EOL;
    }
    print "</table>" . PHP_EOL;
    $stmt->close();
    

      

      

  • 相关阅读:
    JavaScript入门基础(三)
    JavaScript入门基础(二)
    Web页面该如何布局
    如何通过SQL创建删除表的索引,UNIQUE KEY
    vim使用大全
    安装vmwaretools后 真机和虚拟机仍不能复制黏贴
    php通用函数html时间文件大小生成随机数
    Centos下安装配置phpmyadmin
    [Leetcode 43] 11 Container With Most Water
    [Leetcode 39] 129 Sum Root to Leaf Numbers
  • 原文地址:https://www.cnblogs.com/dafei4/p/11227697.html
Copyright © 2011-2022 走看看