PHP PDO的setAttribute函数

数据库的连接:

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.PDO::ATTR_CASE :强制列名为指定的大小写。

 1)PDO::CASE_UPPER :强制列名大写。

1 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
2 $stmt = $pdo->prepare("SELECT * FROM card_users");
3 $stmt->setFetchMode(PDO::FETCH_OBJ);
4 $stmt->execute();
5 $queries = $stmt->fetch();
6 
7 file_put_contents("log.txt",print_r($queries,true));
View Code

结果:

1 stdClass Object
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

2)PDO::CASE_LOWER :强制列名小写。

1 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
2 $stmt = $pdo->prepare("SELECT * FROM card_users");
3 $stmt->setFetchMode(PDO::FETCH_OBJ);
4 $stmt->execute();
5 $queries = $stmt->fetch();
6 
7 file_put_contents("log.txt",print_r($queries,true));
View Code

结果:

1 stdClass Object
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)PDO::CASE_NATURAL :保留数据库驱动返回的列名。

1 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
2 $stmt = $pdo->prepare("SELECT * FROM card_users");
3 $stmt->setFetchMode(PDO::FETCH_OBJ);
4 $stmt->execute();
5 $queries = $stmt->fetch();
View Code

结果:

1 stdClass Object
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
posted @ 2016-12-19 14:39  GoodByeZ  阅读(213)  评论(0编辑  收藏  举报