对于PDO返回的数组重复值(values are repeated for array returned by PDO)

编程入门 行业动态 更新时间:2024-10-28 04:20:32
对于PDO返回的数组重复值(values are repeated for array returned by PDO)

我使用PDO连接到MySQL数据库并获取所有数据。 当我打印出数组时,重复这些值。 怎么解决? 谢谢

连接到DB:

$db = new PDO("mysql:host=localhost:3306;dbname=$dbName", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

获取数据并打印出结果:

$result = $db->query("select UserBirthday, UserAddress, UserZipCode, UserPhone, UserFirstName, UserLastName, UserPassword, UserSecurityQuestion from USER_PROFILE where UserID=$userID;")->fetchAll(); print_r($result);

打印出来的内容:

Array ( [UserBirthday] => 1999-01-01 [0] => 1999-01-01 [UserAddress] => 1 Infinite Loop Seattle [1] => 1 Infinite Loop Seattle [UserZipCode] => 98125 [2] => 98125 [UserPhone] => 2068874596 [3] => 2068874596 [UserFirstName] => abc [4] => abc [UserLastName] => cdf [5] => cdf [UserPassword] => 5271593ca406362d7a2701e331408ab77d5b5b88 [6] => 5271593ca406362d7a2701e331408ab77d5b5b88 [UserSecurityQuestion] => null [7] => null)

I connect to MySQL database using PDO and fetch all the data. When I print out the array, the values are repeated. How to fix it? Thank you

connect to DB:

$db = new PDO("mysql:host=localhost:3306;dbname=$dbName", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

fetch data and print out the result:

$result = $db->query("select UserBirthday, UserAddress, UserZipCode, UserPhone, UserFirstName, UserLastName, UserPassword, UserSecurityQuestion from USER_PROFILE where UserID=$userID;")->fetchAll(); print_r($result);

What it prints out:

Array ( [UserBirthday] => 1999-01-01 [0] => 1999-01-01 [UserAddress] => 1 Infinite Loop Seattle [1] => 1 Infinite Loop Seattle [UserZipCode] => 98125 [2] => 98125 [UserPhone] => 2068874596 [3] => 2068874596 [UserFirstName] => abc [4] => abc [UserLastName] => cdf [5] => cdf [UserPassword] => 5271593ca406362d7a2701e331408ab77d5b5b88 [6] => 5271593ca406362d7a2701e331408ab77d5b5b88 [UserSecurityQuestion] => null [7] => null)

最满意答案

默认情况下,PDO将按名称和索引获取列。

使用PDO::FETCH_ASSOC获取模式只按名称获取:

$result = $db->query("select UserBirthday, UserAddress, UserZipCode, UserPhone, UserFirstName, UserLastName, UserPassword, UserSecurityQuestion from USER_PROFILE where UserID=$userID;")->fetchAll(PDO::FETCH_ASSOC);

另请参阅替代提取模式 。

By default PDO will fetch columns by name AND index.

Use the PDO::FETCH_ASSOC fetch mode to just fetch by name:

$result = $db->query("select UserBirthday, UserAddress, UserZipCode, UserPhone, UserFirstName, UserLastName, UserPassword, UserSecurityQuestion from USER_PROFILE where UserID=$userID;")->fetchAll(PDO::FETCH_ASSOC);

Also check out alternative fetch modes.

更多推荐

本文发布于:2023-08-01 11:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   values   PDO   returned   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!