[英]PHP why does my function return twice the result of the array with different keys?
I've a problem with one of my function in PHP. 我在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. 这是因为默认情况下,php返回一个数组,其中所有数据都有文本和数字索引。 To keep only the textual index, passe PDO::FETCH_ASSOC in the fechAll function like that :
stmt->fetchAll(PDO::FETCH_ASSOC);
要仅保留文本索引,请在fechAll函数中传递PDO :: FETCH_ASSOC:
stmt->fetchAll(PDO::FETCH_ASSOC);
Add the argument PDO::FETCH_ASSOC like so: 像这样添加参数PDO :: FETCH_ASSOC:
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
(You can see the different fetch styles here: http://www.php.net/manual/en/pdostatement.fetch.php ) (你可以在这里看到不同的获取样式: http : //www.php.net/manual/en/pdostatement.fetch.php )
Read up on the PDOStatement::fetch methods . 阅读PDOStatement :: fetch方法 。 The first argument is a "fetch style," which defaults to fetching an array that looks like the above.
第一个参数是“获取样式”,默认为获取类似于上面的数组。 If you just want an array that maps field names to valuse, use:
如果您只想要一个将字段名称映射到valuse的数组,请使用:
$result = $bdd.fetchAll(PDO::FETCH_ASSOC);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.