PHP为什么我的函数用不同的键返回两次数组的结果? [英] PHP why does my function return twice the result of the array with different keys?
问题描述
我在PHP中遇到了一个问题。
使用不同的键返回结果两次...
我只想要一次没有数字键的结果。
这个查询函数返回下列数组:
<?php
#查询
$ typo = GetRowsParams (SELECT * FROM glyphs WHERE g_OK = 1);
函数
函数GetRowsParams($ requete,$ params = array())
{
global $ bdd;
$ stmt = $ bdd-> prepare($ requete)或die(print_r($ req-> errorinfo()));;
$ stmt-> execute($ params);
$ result = $ stmt-> fetchAll();
返回$ result;
}
?>
#Array
Array(
[0] => Array(
[g_ID] => 1
[0 ] => 1
[g_name] => zero_Nagar.svg
[1] => zero_Nagar.svg
[g_height] => 1174
[2] = > 1174
[g_width] => 352
[3] => 352
[g_tag] => Test
[4] => Test
[g_u_ID] => 2
[5] => 2
[g_path] => 02uploads /Test/ zero_Nagar.svg
[6] => zero_Nagar.svg
[g_path_PNG] => 02uploads / Test / zero_Nagar.png
[7] => 02uploads / Test / zero_Nagar.png
[g_OK] => 1
$ b [1] =>数组(
[g_ID] => 2
[0] => 2
[g_name] => A
Nagar.svg [1] => A
...
...
为什么每行都用不同的键显示两次?我的错误在哪里?
感谢您的帮助...
这是因为默认情况下,php返回一个数组,其中所有数据都有文本和数字索引。
仅保留文本索引,在fechAll函数中传递PDO :: FETCH_ASSOC,如下所示:
stmt-> fetchAll(PDO :: FETCH_ASSOC);
I've a problem with one of my function in PHP.
It returns the result two times with different keys…
I want the result only one time without the number keys.
This query and function returns the following array:
<?php
#The query
$typo = GetRowsParams("SELECT * FROM glyphs WHERE g_OK=1");
#The function
function GetRowsParams($requete, $params = array())
{
global $bdd;
$stmt = $bdd->prepare($requete) or die(print_r($req->errorinfo()));;
$stmt->execute($params);
$result = $stmt->fetchAll();
return $result;
}
?>
# The Array
Array (
[0] => Array (
[g_ID] => 1
[0] => 1
[g_name] => zero_Nagar.svg
[1] => zero_Nagar.svg
[g_height] => 1174
[2] => 1174
[g_width] => 352
[3] => 352
[g_tag] => Test
[4] => Test
[g_u_ID] => 2
[5] => 2
[g_path] => 02uploads/Test/zero_Nagar.svg
[6] => 02uploads/Test/zero_Nagar.svg
[g_path_PNG] => 02uploads/Test/zero_Nagar.png
[7] => 02uploads/Test/zero_Nagar.png
[g_OK] => 1
[8] => 1
)
[1] => Array (
[g_ID] => 2
[0] => 2
[g_name] => A
Nagar.svg [1] => A
…
…
Why each row is displayed twice with a different key? Where is my mistake?
Thank you for your help…
it is because by default php returns an array where all data has a textual AND a numeric index.
To keep only the textual index, passe PDO::FETCH_ASSOC in the fechAll function like that :
stmt->fetchAll(PDO::FETCH_ASSOC);
这篇关于PHP为什么我的函数用不同的键返回两次数组的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!