3

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…

3
  • your fetsch all have to use the mysql_fetch_assoc Commented Mar 1, 2012 at 13:30
  • i think there is no mistake. result set always have the values in this way so you can fetch your result either using column names or their indexes. Commented Mar 1, 2012 at 13:32
  • @silly: he's using PDO, not the mysql extension. Commented Mar 1, 2012 at 13:33

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.