zoukankan      html  css  js  c++  java
  • php怎么访问数据库

    三种方式
    1.函数方式
    2.面向对象方式
    3.PDO方式

    1.创建连接对象   $db = new MySQLi("localhost","root","123","mydb");

    2.判断当前连接是否正确

    if(mysqli_connect_error()){

    echo "连接失败!";
    exit;
    }

    3.写一个SQL语句   $sql = "select * from info";

    4.执行SQL语句,如果成功返回一个结果集对象   $result = $db->query($sql);

    5.读取查询结果if($result){

    从结果集对象里面读取所有数据,返回二维数组
    $arr = $result->fetch_all();
    从结果集中读取数据,每次读一条,返回一维数组(索引关联都有)
    $arr = $result->fetch_array();
    从结果中读取数据,每次读一条,返回一维数组(关联)
    $arr = $result->fetch_assoc();
    从结果中读取数据,每次读一条,返回是一个对象
    $arr = $result->fetch_object();
    从结果中读取数据,每次读一条,返回一维数组(索引)
    $arr = $result->fetch_row();

    while($arr = $result->fetch_row()){
    var_dump($arr);
    }}

      $db = new MySQLi("localhost","root","123","mydb");
      mysqli_connect_error()?die("连接失败!"):"";
      $sql = "delete from nation where code='n006'";
      if($db->query($sql)){
        echo "删除成功!";
      }else{
      echo "删除失败!";
      }

    ?>

    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td>代号</td>
    <td>名称</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    </tr>

    <?php
      $db = new MySQLi("localhost","root","123","mydb");//
      $sql = "select * from info";//查询语句
      $result = $db->query($sql);//执行查询语句返回结果集查询
      ($result){
        $arr = $result->fetch_all();//从结果集对象里面读取所有数据,返回二维数组
      foreach($arr as $v){//遍历数组
        echo "<tr>
          <td>{$v[0]}</td>
          <td>{$v[1]}</td>
          <td>{$v[2]}</td>
          <td>{$v[3]}</td>
          <td>{$v[4]}</td>
          </tr>";
        }
        }
        ?>
    </table>

  • 相关阅读:
    【贪心+DFS】D. Field expansion
    【贪心+博弈】C. Naming Company
    【dp】E. Selling Souvenirs
    【multimap的应用】D. Array Division
    内存变量边界对齐
    3.8 高级检索方式(二)
    3.7 高级检索方式(一)
    openGL加载obj文件+绘制大脑表层+高亮染色
    3.6 Lucene基本检索+关键词高亮+分页
    3.5 实例讲解Lucene索引的结构设计
  • 原文地址:https://www.cnblogs.com/forqiwen/p/8324219.html
Copyright © 2011-2022 走看看