PDO returning incorrect, but duplicate, data. Key's not in database.










12















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.



Result:



[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)


The query is quite simple:



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


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?










share|improve this question






















  • before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

    – rkeet
    Apr 29 '13 at 9:12















12















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.



Result:



[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)


The query is quite simple:



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


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?










share|improve this question






















  • before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

    – rkeet
    Apr 29 '13 at 9:12













12












12








12


0






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.



Result:



[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)


The query is quite simple:



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


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?










share|improve this question














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.



Result:



[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)


The query is quite simple:



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


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?







php pdo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 29 '13 at 9:11









rkeetrkeet

1,89621735




1,89621735












  • before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

    – rkeet
    Apr 29 '13 at 9:12

















  • before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

    – rkeet
    Apr 29 '13 at 9:12
















before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

– rkeet
Apr 29 '13 at 9:12





before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.

– rkeet
Apr 29 '13 at 9:12












3 Answers
3






active

oldest

votes


















22














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.



Use like so:



$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes


fetchAll docs
fetch docs






share|improve this answer




















  • 2





    Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

    – rkeet
    Apr 29 '13 at 9:18











  • @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

    – Yogesh Suthar
    Apr 29 '13 at 9:20






  • 1





    Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

    – rkeet
    Apr 29 '13 at 9:21



















9














This is not duplicate data fetchAll returns data in numeric array as well as associative array.



See the Docs



Use this for retrieving only associative array



return $query->fetchAll(PDO::FETCH_ASSOC);





share|improve this answer






























    -1














    seems to do the trick for me. Put this on top



    use IlluminateDatabaseEventsStatementPrepared;


    when ever fetching include pdo::fetch_assoc



    return($pdo->fetchAll(PDO::FETCH_ASSOC));





    share|improve this answer























    • Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

      – rkeet
      Nov 14 '18 at 7:30










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16274963%2fpdo-returning-incorrect-but-duplicate-data-keys-not-in-database%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    22














    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.



    Use like so:



    $query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
    $query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes


    fetchAll docs
    fetch docs






    share|improve this answer




















    • 2





      Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

      – rkeet
      Apr 29 '13 at 9:18











    • @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

      – Yogesh Suthar
      Apr 29 '13 at 9:20






    • 1





      Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

      – rkeet
      Apr 29 '13 at 9:21
















    22














    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.



    Use like so:



    $query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
    $query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes


    fetchAll docs
    fetch docs






    share|improve this answer




















    • 2





      Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

      – rkeet
      Apr 29 '13 at 9:18











    • @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

      – Yogesh Suthar
      Apr 29 '13 at 9:20






    • 1





      Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

      – rkeet
      Apr 29 '13 at 9:21














    22












    22








    22







    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.



    Use like so:



    $query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
    $query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes


    fetchAll docs
    fetch docs






    share|improve this answer















    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.



    Use like so:



    $query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
    $query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes


    fetchAll docs
    fetch docs







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 29 '16 at 16:58

























    answered Apr 29 '13 at 9:14









    Emmanuel OkekeEmmanuel Okeke

    1,192812




    1,192812







    • 2





      Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

      – rkeet
      Apr 29 '13 at 9:18











    • @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

      – Yogesh Suthar
      Apr 29 '13 at 9:20






    • 1





      Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

      – rkeet
      Apr 29 '13 at 9:21













    • 2





      Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

      – rkeet
      Apr 29 '13 at 9:18











    • @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

      – Yogesh Suthar
      Apr 29 '13 at 9:20






    • 1





      Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

      – rkeet
      Apr 29 '13 at 9:21








    2




    2





    Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

    – rkeet
    Apr 29 '13 at 9:18





    Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.

    – rkeet
    Apr 29 '13 at 9:18













    @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

    – Yogesh Suthar
    Apr 29 '13 at 9:20





    @Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)

    – Yogesh Suthar
    Apr 29 '13 at 9:20




    1




    1





    Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

    – rkeet
    Apr 29 '13 at 9:21






    Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.

    – rkeet
    Apr 29 '13 at 9:21














    9














    This is not duplicate data fetchAll returns data in numeric array as well as associative array.



    See the Docs



    Use this for retrieving only associative array



    return $query->fetchAll(PDO::FETCH_ASSOC);





    share|improve this answer



























      9














      This is not duplicate data fetchAll returns data in numeric array as well as associative array.



      See the Docs



      Use this for retrieving only associative array



      return $query->fetchAll(PDO::FETCH_ASSOC);





      share|improve this answer

























        9












        9








        9







        This is not duplicate data fetchAll returns data in numeric array as well as associative array.



        See the Docs



        Use this for retrieving only associative array



        return $query->fetchAll(PDO::FETCH_ASSOC);





        share|improve this answer













        This is not duplicate data fetchAll returns data in numeric array as well as associative array.



        See the Docs



        Use this for retrieving only associative array



        return $query->fetchAll(PDO::FETCH_ASSOC);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 29 '13 at 9:13









        Yogesh SutharYogesh Suthar

        26.8k175891




        26.8k175891





















            -1














            seems to do the trick for me. Put this on top



            use IlluminateDatabaseEventsStatementPrepared;


            when ever fetching include pdo::fetch_assoc



            return($pdo->fetchAll(PDO::FETCH_ASSOC));





            share|improve this answer























            • Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

              – rkeet
              Nov 14 '18 at 7:30















            -1














            seems to do the trick for me. Put this on top



            use IlluminateDatabaseEventsStatementPrepared;


            when ever fetching include pdo::fetch_assoc



            return($pdo->fetchAll(PDO::FETCH_ASSOC));





            share|improve this answer























            • Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

              – rkeet
              Nov 14 '18 at 7:30













            -1












            -1








            -1







            seems to do the trick for me. Put this on top



            use IlluminateDatabaseEventsStatementPrepared;


            when ever fetching include pdo::fetch_assoc



            return($pdo->fetchAll(PDO::FETCH_ASSOC));





            share|improve this answer













            seems to do the trick for me. Put this on top



            use IlluminateDatabaseEventsStatementPrepared;


            when ever fetching include pdo::fetch_assoc



            return($pdo->fetchAll(PDO::FETCH_ASSOC));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 3:01









            gritgrit

            9




            9












            • Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

              – rkeet
              Nov 14 '18 at 7:30

















            • Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

              – rkeet
              Nov 14 '18 at 7:30
















            Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

            – rkeet
            Nov 14 '18 at 7:30





            Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.

            – rkeet
            Nov 14 '18 at 7:30

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16274963%2fpdo-returning-incorrect-but-duplicate-data-keys-not-in-database%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto