Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对PDO返回的数组重复值_Php_Mysql_Pdo - Fatal编程技术网

Php 对PDO返回的数组重复值

Php 对PDO返回的数组重复值,php,mysql,pdo,Php,Mysql,Pdo,我使用PDO连接到MySQL数据库并获取所有数据。当我打印出数组时,这些值会重复。如何修复它?多谢各位 连接到数据库: $db = new PDO("mysql:host=localhost:3306;dbname=$dbName", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $db->setAttribute(PDO::ATTR_ERRMOD

我使用PDO连接到MySQL数据库并获取所有数据。当我打印出数组时,这些值会重复。如何修复它?多谢各位

连接到数据库:

$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)

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

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

$result = $db->query("select UserBirthday, UserAddress, UserZipCode, UserPhone, UserFirstName, 
            UserLastName, UserPassword, UserSecurityQuestion
            from USER_PROFILE
            where UserID=$userID;")->fetchAll(PDO::FETCH_ASSOC);
另外,请查看备选方案。

正确的方法

$dsn = "mysql:host=localhost;dbname=$dbName;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db  = new PDO($dsn,$user, $pass, $opt);

$stm = $db->prepare("select * FROM USER_PROFILE where UserID=?");
$stm->execute(array($userID));
$data = $stm->fetch(); 
print_r($data);

哦,非常感谢。但是为什么会发生这种情况呢?PDOs默认的获取模式是PDO::fetch_Duth,它同时包含列名和数字索引。假设
PDO::fetch_ASSOC
是主要使用的获取模式,这非常优雅+1.