問題描述
我剛開始使用 $pdo 語句,所以可能很簡單,我還沒有在 php.net 上閱讀過.我在查詢數據庫時收到重復的結果.
I'm new to using $pdo statements so might be something simple I haven't yet read on php.net. I'm receiving duplicate results when querying the database.
結果:
[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)
查詢很簡單:
function getData(){
global $pdo;
$query = $pdo->prepare('SELECT * FROM usermeta WHERE meta_key = "fbmeta" LIMIT 0,30');
$query->execute();
return $query->fetchAll();
}
print_r( getData() );
問題是命名鍵 (umeta_id
, user_id
, meta_key
, meta_value
) 確實存在,數字鍵沒有.查詢如何返回這些?我該如何防止它們被退回?
The problem is that the named keys (umeta_id
, user_id
, meta_key
, meta_value
) DO exist, the numeric keys do not. How come the query returns these? And how do I prevent them from even being returned?
推薦答案
它不是重復的,它只是您正在使用的當前 FETCH_MODE
.要獲得關聯鍵,您只需要這樣指定;默認情況下,它同時獲取.
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
像這樣使用:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll 文檔
獲取文檔
這篇關于PDO 返回不正確但重復的數據.密鑰不在數據庫中.的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!