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");
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));
结果:
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 )
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));
结果:
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 )
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();
结果:
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 )
学习记录,方便复习