• PHP PDO的FETCH_NUM、FETCH_BOTH、FETCH_ASSOC


    不容易混淆的有:FETCH_CLASS,FETCH_COLUMN,FETCH_OBJ...

    数据库的连接准备部分

    1 $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=cardslg';
    2 $username = 'root';
    3 $password = '';
    4 $options = array(
    5     PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    6 );
    7 $pdo = new PDO($dsn,$username,$password,$options);
    8 
    9 $stmt = $pdo->prepare("SELECT * FROM card_users");
    View Code

    1.FETCH_NUM

    1 $stmt->setFetchMode(PDO::FETCH_NUM);
    2 $stmt->execute();
    3 $queries = $stmt->fetch();
    4 
    5 file_put_contents("log.txt",print_r($queries,true));
    View Code

    结果:

    1 Array
    2 (
    3     [0] => 1
    4     [1] => lvpeilin
    5     [2] => 10
    6     [3] => 1
    7     [4] => 2016-12-16 11:55:30
    8 )
    View Code

    2.FETCH_ASSOC

    1 $stmt->setFetchMode(PDO::FETCH_ASSOC);
    2 $stmt->execute();
    3 $queries = $stmt->fetch();
    4 
    5 file_put_contents("log.txt",print_r($queries,true));
    View Code

    结果:

    1 Array
    2 (
    3     [id] => 1
    4     [user_name] => lvpeilin
    5     [vip] => 10
    6     [consortium_id] => 1
    7     [created_at] => 2016-12-16 11:55:30
    8 )
    View Code

    3.FETCH_BOTH

    1 $stmt->setFetchMode(PDO::FETCH_BOTH);
    2 $stmt->execute();
    3 $queries = $stmt->fetch();
    4 
    5 file_put_contents("log.txt",print_r($queries,true));
    View Code

    结果:

     1 Array
     2 (
     3     [id] => 1
     4     [0] => 1
     5     [user_name] => lvpeilin
     6     [1] => lvpeilin
     7     [vip] => 10
     8     [2] => 10
     9     [consortium_id] => 1
    10     [3] => 1
    11     [created_at] => 2016-12-16 11:55:30
    12     [4] => 2016-12-16 11:55:30
    13 )
    View Code
    学习记录,方便复习
  • 相关阅读:
    关于编码的两个小点(摘)
    c#中类和成员的修饰符介绍
    jquery/js不支持ie9以下版本的方法或属性
    一次Linux系统被攻击的分析过程
    运维堡垒机(跳板机)系统 python
    puppet 3+Unicorn+Nginx安装配置
    用memcache来同步session
    php+memcache实现的网站在线人数统计
    CentOS yum 源的配置与使用
    Bind+DLZ构建企业智能DNS/DNS
  • 原文地址:https://www.cnblogs.com/jingjingdidunhe/p/6197276.html
Copyright © 2020-2023  润新知