zoukankan      html  css  js  c++  java
  • PDO操作

    1.创建实例与取结果集

    <?
    $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $rs = $db->query("SELECTaa,bb,cc FROM foo");
    while ($arr = $rs->fetch()) {
         //...
    }
    ?>
    

     2.取一个字段结果

    <?php
    $rs = $db->query("SELECT  COUNT(*) FROM foo");
    $col = $rs->fetchColumn();
    echo  $col;
    ?>

    3.prepare

    <?php
    /* Execute a prepared statement by passing an array of values */
    $sth = $dbh->prepare('SELECT name, colour, calories
        FROM fruit
        WHERE calories < ? AND colour = ?');
    $sth->execute(array(150, 'red'));
    $red = $sth->fetchAll();
    $sth->execute(array(175, 'yellow'));
    $yellow = $sth->fetchAll();
    ?>

    4.检查链接

    <?php
    try {
    $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $db = null;
    } catch (PDOException $e) {
    print "Error: " . $e->getMessage() . "<br/>";
    die();
    }
    ?>

    5.返回值

    PDO::exec() - Execute an SQL statement and return the number of affected rows   //返回受影响的行数
    PDO::query() - Executes an SQL statement, returning a result set as a PDOStatement object   //返回结果集对象
    $rs = $db->query("SELECT * FROM test_table");
    echo $rs->rowCount();     //取结果集对象的行数
  • 相关阅读:
    反反爬 | 如何巧过 CloudFlare 5秒盾?
    Xpath高级用法
    GZIP 头解析
    学习S5
    Chrome 建立SOCKS5代理
    建立IP6隧道
    linux 配置Socks51
    linux 配置Socks5
    最近买了一个域名 哈哈,棒棒哒~~
    jquery 动态添加下拉框 需要增加 煊染 selectmenu("refresh");
  • 原文地址:https://www.cnblogs.com/fenle/p/4874800.html
Copyright © 2011-2022 走看看