zoukankan      html  css  js  c++  java
  • 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
    学习记录,方便复习
  • 相关阅读:
    TOJ 5021: Exchange Puzzle
    Educational Codeforces Round 26
    2017 Multi-University Training Contest
    TOJ 5020: Palindromic Paths
    数论之 莫比乌斯函数
    TOJ 4475: The Coolest Sub-matrix
    Game on Tree
    python 线程
    python 管道、数据共享、进程池
    python 守护进程、同步锁、信号量、事件、进程通信Queue
  • 原文地址:https://www.cnblogs.com/jingjingdidunhe/p/6197276.html
Copyright © 2011-2022 走看看