Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Using Fetch on the same query multiple times

    I'm working on writing a basic PHP book, and I'm trying to code an example that will run different types of MySQLi fetch functions on the same query. The issue is the query is only giving the results to the first fetch function. Is there any way to give the results to all the fetch functions without duplicating the query before each individual fetch function? If I duplicate the query before each fetch function all works fine, I just don't want to have everybody type the same query over and over again; there's gotta be a better way.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    require 
    'db_connect.php';

    $query_result=mysqli_query($db"SELECT `username`, `password` FROM `users` 
    WHERE `username`='Vega'"
    );
    $fetch_associative_array=mysqli_fetch_assoc($query_result);
    $fetch_numeric_array=mysqli_fetch_row($query_result);
    $fetch_numeric_array_alternate=mysqli_fetch_array($query_resultMYSQLI_NUM);
    $fetch_associative_array_alternate=mysqli_fetch_array($query_resultMYSQLI_ASSOC);
    $fetch_array=mysqli_fetch_array($query_resultMYSQLI_BOTH);

    echo 
    "<p>Associative Array </p>
    <p>Your Username is: {$fetch_associative_array['username']}</p>
    <p>Your hashed password is: {$fetch_associative_array['password']}</p>"
    ;

    echo 
    "<p>Numeric Array </p>
    <p>Your Username is: {$fetch_numeric_array[0]}</p>
    <p>Your hashed password is: {$fetch_numeric_array[1]}</p>"
    ;

    echo 
    "<p>Alternative Numeric Array </p>
    <p>Your Username is: {$fetch_numeric_array_alternate[0]}</p>
    <p>Your hashed password is: {$fetch_numeric_array_alternate[1]}</p>"
    ;

    echo 
    "<p>Alternative Associative Array </p>
    <p>Your Username is: {$fetch_associative_array_alternate['username']}</p>
    <p>Your hashed password is: {$fetch_associative_array_alternate['password']}</p>"
    ;

    echo 
    "<p>Associative/Numeric Array </p>
    <p>Your Username is: {$fetch_array['username']} - Using Associative</p>
    <p>Your hashed password is: {$fetch_array[1]} - Using Numeric </p>
    "
    ;
    ?>
    Last edited by HDRebel88; 07-14-2012 at 10:26 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    All you have to do is rewind it. In MySQLi you would issue:
    PHP Code:
    mysqli_data_seek($query_result0);
    // or, for OO:
    $query_result->data_seek(0); 
    Then you can issue a fetch again.
    Obviously with this type of handling this would be infeasible (except in a learning situation like above). All you'd need to use is the mysql_fetch_array with the default or MYSQLI_BOTH to fetch the same data you need and cover for every case scenario.

  • Users who have thanked Fou-Lu for this post:

    HDRebel88 (07-14-2012)

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Thanks, that will definitely do it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •