zoukankan      html  css  js  c++  java
  • mysqli使用

    <?php

    ////判断数据库是否连接成功

    $connection = mysqli_connect('localhost', 'root', '12345678', 'ceshi', 3306 );
    if ( $connection ) {
    echo "数据库连接成功"."<br>";
    }else {
    echo "数据库连接失败"."<br>";
    }
    ?>


    <?php

    ////查询表中数据,并且输出

    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 );
    $query="select * from dunling_chat";
    $result=$mysqli->query($query);
    if ($result) {
    if($result->num_rows>0){ //判断结果集中行的数目是否大于0
    while($row =$result->fetch_array() ){ //循环输出结果集中的记录
    echo ($row[0])."<br>";
    echo ($row[1])."<br>";
    echo ($row[2])."<br>";
    echo ($row[3])."<br>";
    echo "<hr>";
    }
    }
    }else {
    echo "查询失败";
    }
    $result->free();
    $mysqli->close();
    ?>


    <?php
    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 ); //实例化mysqli
    $query="select * from dunling_chat";
    $result=$mysqli->prepare($query); //进行预准备语句查询
    $result->execute(); //执行预准备语句
    $result->bind_result($id,$nicheng,$content,$time); //绑定结果
    while ($result->fetch()) {
    echo $id."<br />";
    echo $nicheng."<br />";
    echo $content."<br />";
    echo $time."<br />";
    }
    $result->close(); //关闭预准备语句
    $mysqli->close(); //关闭连接
    ?>


    <?php
    /////插入一个数据
    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 ); //实例化mysqli
    $query="insert into dunling_chat (id,nicheng,content,time) values (?,?,?,?)";
    $result=$mysqli->prepare($query);
    $result->bind_param("isss",$id,$nicheng,$content,$time); //绑定参数
    $id=9;
    $nicheng='1234';
    $content='好';
    $time='2019-8-1';
    $result->execute(); //执行预准备语句
    $result->close();
    $mysqli->close();
    ?>


    <?php
    ///// 可以同时绑定参数和绑定结果
    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 ); //实例化mysqli
    $query="select * from dunling_chat where id < ?";
    $result=$mysqli->prepare($query);
    $result->bind_param("i",$id); //绑定参数
    $id=3;
    $result->execute();
    $result->bind_result($id,$nicheng,$content,$time); //绑定结果
    while ($result->fetch()) {
    echo $id."<br>";
    echo $nicheng."<br>";
    echo $content."<br>";
    echo $time."<br>";
    }
    $result->close();
    $mysqli->close();
    ?>

    <?php

    ////多个查询
    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'think', 3306 ); //实例化mysqli
    $query = "select id from think_user ;";
    $query .= "select id from think_data ";
    if ($mysqli->multi_query($query)) { //执行多个查询
    do {
    if ($result = $mysqli->store_result()) {
    while ($row = $result->fetch_row()) {
    echo $row[0];
    echo "<br>";
    }
    $result->close();
    }
    if ($mysqli->more_results()) {
    echo ("-----------------<br>"); //连个查询之间的分割线
    }
    } while ($mysqli->next_result());
    }
    $mysqli->close();//关闭连接
    ?>

    <?php
    $id=$_GET['id'];
    ///// 参数 传参 可以同时绑定参数和绑定结果
    $mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 ); //实例化mysqli
    $query="select * from dunling_chat where id < ?";
    $result=$mysqli->prepare($query);
    $result->bind_param("i",$id); //绑定参数

    $result->execute();
    $result->bind_result($id,$nicheng,$content,$time); //绑定结果
    while ($result->fetch()) {
    echo $id."<br>";
    echo $nicheng."<br>";
    echo $content."<br>";
    echo $time."<br>";
    }
    $result->close();
    $mysqli->close();
    ?>

  • 相关阅读:
    Spring实现AOP
    js Form表单转json格式,及后台接收(多种方法)
    Java 网络编程
    分布式系统学习
    java消息中间件
    oracle Clob类型转换成String类型
    Oracle的CLOB大数据字段类型
    oracle wm_concat函数 列转行 分组函数
    Oracle trunc函数使用
    ajax异步提交文件
  • 原文地址:https://www.cnblogs.com/summerGraden/p/11434698.html
Copyright © 2011-2022 走看看