Page 1 of 1

Reusing a Data query result set

Posted: Mon Jul 26, 2010 12:59 am
by Nazum
Hi all,

I'm relatively a novice to php(I've worked mainly in ASP.NET in the past) and therefore using the mysql and/or mysqli libraries. I have a result set that is very small and I'm using it to populate a html select list like so:

Code: Select all

<?php 
$conn = mysqli_connect(.....);
$sql = "select id, name
                    from client
                    order by name";

$result = $conn->query($sql) or die("Database Error:  " . mysqli_error($conn));
?>
.
.
.
<select name="client" id="client">
                        <?php
                        while($row = $result->fetch_array())
                        {
                            echo"<option value =".$row['id'].">".$row['name']."</option>";     
                        }
                        ?>
                    </select>
As you can see I'm making a list of clients. This is for a work order/time entry system. The problem is that I would like to reuse the result set and make a second list of clients for another data field namely billable clients.

If I understand $result->fetch_array() correctly the result set is iterated through or else consumed like stack's pop(). I'd either like to reset the result set so I can start back at the beginning or else make a copy of it so I can pull data out of the copy. I'd prefer to not run the query twice if I don't have to.

Thanks,

-Jordan

Re: Reusing a Data query result set

Posted: Mon Jul 26, 2010 1:18 am
by Christopher

Code: Select all

// results into an array
                        $rows = array();
                        while($row = $result->fetch_array())
                        {
                            $rows[] = $row;     
                        }
// and later
                        foreach ($rows as $row)
                        {
                            echo"<option value =".$row['id'].">".$row['name']."</option>";     
                        }

Re: Reusing a Data query result set

Posted: Mon Jul 26, 2010 3:10 am
by VladSun
Or use mysql_data_seek function to rewind the result.