PHP PDO的setAttribute函数

数据库的连接:

 $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=cardslg';
$username = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$pdo = new PDO($dsn,$username,$password,$options); $stmt = $pdo->prepare("SELECT * FROM card_users");

1.PDO::ATTR_CASE :强制列名为指定的大小写。

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

 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
$stmt = $pdo->prepare("SELECT * FROM card_users");
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->execute();
$queries = $stmt->fetch(); file_put_contents("log.txt",print_r($queries,true));

结果:

 stdClass Object
(
[ID] => 1
[USER_NAME] => lvpeilin
[VIP] => 10
[CONSORTIUM_ID] => 1
[CREATED_AT] => 2016-12-16 11:55:30
)

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

 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
$stmt = $pdo->prepare("SELECT * FROM card_users");
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->execute();
$queries = $stmt->fetch(); file_put_contents("log.txt",print_r($queries,true));

结果:

 stdClass Object
(
[id] => 1
[user_name] => lvpeilin
[vip] => 10
[consortium_id] => 1
[created_at] => 2016-12-16 11:55:30
)

3)PDO::CASE_NATURAL :保留数据库驱动返回的列名。

 $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
$stmt = $pdo->prepare("SELECT * FROM card_users");
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->execute();
$queries = $stmt->fetch();

结果:

 stdClass Object
(
[id] => 1
[user_name] => lvpeilin
[vip] => 10
[consortium_id] => 1
[created_at] => 2016-12-16 11:55:30
)
上一篇:ubuntu下Qt cannot find -lGL错误的解决方法 (转载)


下一篇:关于Ubuntu下is not in the sudoers file解决方法