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

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $dbname = "myDB";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // 输出数据
        while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        }
    } else {
        echo "0 结果";
    }
    $conn->close();
    ?>

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
     
    // 创建连接
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("连接失败: " . mysqli_connect_error());
    }
     
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = mysqli_query($conn, $sql);
     
    if (mysqli_num_rows($result) > 0) {
        // 输出数据
        while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        }
    } else {
        echo "0 结果";
    }
     
    mysqli_close($conn);
    ?>
    <?php
    echo "<table style='border: solid 1px black;'>";
    echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
     
    class TableRows extends RecursiveIteratorIterator {
        function __construct($it) { 
            parent::__construct($it, self::LEAVES_ONLY); 
        }
     
        function current() {
            return "<td style='150px;border:1px solid black;'>" . parent::current(). "</td>";
        }
     
        function beginChildren() { 
            echo "<tr>"; 
        } 
     
        function endChildren() { 
            echo "</tr>" . "
    ";
        } 
    } 
     
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDBPDO";
     
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests"); 
        $stmt->execute();
     
        // 设置结果集为关联数组
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
        foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
            echo $v;
        }
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
    echo "</table>";
    ?>
  • 相关阅读:
    c++关键字static的作用
    react 中echarts-for-react使用 自适应div
    react Echart 自适应问题
    react 中Echarts不自适应问题
    解决 react typescript 中 antD 走马灯 this.slider 报错
    antD 走马灯跳到指定页面
    js 判断语句 或的写法
    ajax 分页点击数据缓存
    react antD moment
    react antD 日期选择
  • 原文地址:https://www.cnblogs.com/tszr/p/10955727.html
Copyright © 2011-2022 走看看