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");
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));
结果:
1 Array 2 ( 3 [0] => 1 4 [1] => lvpeilin 5 [2] => 10 6 [3] => 1 7 [4] => 2016-12-16 11:55:30 8 )
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));
结果:
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 )
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));
结果:
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 )
学习记录,方便复习