Reputation: 1029
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…
Upvotes: 3
Views: 1167
Reputation: 1597
Read up on the PDOStatement::fetch methods. 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:
$result = $bdd.fetchAll(PDO::FETCH_ASSOC);
Upvotes: 0
Reputation: 3713
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);
Upvotes: 3
Reputation: 3500
Add the argument PDO::FETCH_ASSOC like so:
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
(You can see the different fetch styles here: http://www.php.net/manual/en/pdostatement.fetch.php)
Upvotes: 2