zoukankan      html  css  js  c++  java
  • PHP数据访问

    //造一个mysqli对象,造连接对象
    $db = new MySQLi("localhost", "用户名", "密码", "dbname");
    
    //准备一条SQL语句
    $sql = "select * from 表名";
    
    //执行SQL语句,如果是查询语句,成功返回结果集对象
    $result = $db->query($sql);
    
    //判断返回是否执行内容
    if ($result) {
        //如果成功了,从结果集对象里面读取数据
    //    $attr = $result->fetch_all();
    //    $attr = $result->fetch_row();//执行一次读一行
    //    $attr = $result->fetch_row();
    //    $attr = $result->fetch_row();
    
    //    while ($attr = $result->fetch_row())//返回索引数组
    //    {
    //        var_dump($attr);
    //
    //    $attr=$result->fetch_assoc();//返回关联数组
    
    //    $attr=$result->fetch_object();//返回对象
    
    //    $attr=$result->fetch_array();//返回的数据既有索引,又有关联的
    
    
    //    var_dump($attr);
    }

    形式1

    <table border="1" width="100%" cellpadding="0" cellspacing="0">
    
        <tr>
            <td>号码</td>
            <td>姓名</td>
            <td>性别</td>
            <td>生日</td>
            <td>职称</td>
            <td>部门</td>
        </tr>
    <?php
    $db=new MySQLi("localhost","root","root","zuoye");
    $sql="select * from teacher";
    $result =$db->query($sql);
    
    if($result)
    {
        while($attr=$result->fetch_row())
        {
            echo"<tr>
            <td>{$attr[0]}</td>
            <td>{$attr[1]}</td>
            <td>{$attr[2]}</td>
            <td>{$attr[3]}</td>
            <td>{$attr[4]}</td>
            <td>{$attr[5]}</td>
        </tr>";
            }
        }
        
    ?>
    </table>

    形式2

    $db = new MySQLi("localhost","root","root","zuoye");
        $sql = "select * from nation";
        $result = $db->query($sql);
        
        echo "<select>";
        
        if($result)
        {
            while($attr = $result->fetch_row())
            {
                echo "<option value='{$attr[0]}'>{$attr[1]}</option>";
            }
        }
        
        echo "</select>";
  • 相关阅读:
    vue Syntax Error: Unexpected token {
    MQ 分拆Json数据包然后上传
    京东商城投诉商家
    C# 读写Txt文件
    DB2时间函数 实现 时间加减
    VS恢复默认设置
    只用一次循环开销 将类似 1 A 、1 B 的数据返回成为 1 A,B 的拼接形式
    DB2 With语句递归
    属性与字段的区别
    With语句在数据统计应用
  • 原文地址:https://www.cnblogs.com/gaobint/p/6415403.html
Copyright © 2011-2022 走看看