zoukankan      html  css  js  c++  java
  • PHP pdao用法总结

    $sql = 'SELECT name, colour, calories
        FROM fruit
        WHERE calories < :calories AND colour = :colour';
    $sth $dbh->prepare($sqlarray(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $sth->execute(array(':calories' => 150, ':colour' => 'red'));
    $red $sth->fetchAll();
    $sth->execute(array(':calories' => 175, ':colour' => 'yellow'));
    $yellow $sth->fetchAll();
    1
    2
    3
    4
    5
    6
    7
    $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();

    总结地址:http://www.cnblogs.com/pinocchioatbeijing/archive/2012/03/20/2407869.html

    http://sjolzy.cn/PDO-query-results-achieved-in-many-ways.html

    无论原来数据库里的数据是什么类型,通过这写遍历方法出来的数组,里面的成员都成了字符串 ? var_dump一下,发现全是string......

    If you want to use PDO::FETCH_CLASS but don't like that all the values are of the type string, you can always use the __construct function of the class specified to convert them to a different type.

    Another way is using mysqlnd, but it seems I had to recompile PHP for that.

    <?php

    class Cdr {
        public $a// int
        public $b// float
        public $c// string
        
        public function __construct() {
            $this->intval($this->a);
            $this->floatval($this->b);
        }
         
    }

    // ...
    $arrCdrs $objSqlStatement->fetchAll(PDO::FETCH_CLASS'Cdr');

    ?>

     setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 

    发现填上这句话后,fetchall得来的类型是正确的。

    如果没有这句话,fetchall得来的都是字符串 

    PDO 指南

    http://www.oschina.net/translate/php-pdo-how-to

  • 相关阅读:
    一周试用yii开发一个带各种该有功能的web程序(三)
    制作centos的U盘启动盘
    VMware下centos6.3minimal搭建网络环境
    [原]SQLite的学习系列之获取数据库版本
    Android Studio NDK 学习之接受Java传入的字符串
    [原]Ubuntu 14.04编译Android Kernel
    [原]运行编译好的Android模拟器
    [原]编译Android源码过程中遇到的问题
    [原]Android打包之Gradle打包
    [原]Android打包之跨平台打包
  • 原文地址:https://www.cnblogs.com/as3lib/p/5443946.html
Copyright © 2011-2022 走看看