Use mysql query twice (seeking resultset to beginning of records)

Many often there are problems where we have to repeat our MySql query to number of times. For this either we have to re execute our query again or set some method that query will be repeated without executing it again. mysql_data_seek() will seek our resultset pointer to the beginning of the recordset.

Example

<!DOCTYPE HTML>
<html>
    <head>
        <title>Untitled Document</title>
    </head>
    <body>
    	<p>Enter your choices</p>
    <?php
    
    // connecting
    $con = mysql_connect('localhost', 'username', 'password');
    
    if (! $con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("test", $con);
    $sql = "SELECT streams FROM app_pref";
    $result = mysql_query($sql);
    
    for ($i = 1; $i < 5; $i ++) // number of times the loop will repeat 
    {
    ?>
    <p> Choice <?php echo $i ?> </p>
    	<select name="chocie <?php echo $i ?>">
        <?php
        while ($row = mysql_fetch_array($result))
        {
                echo $row['streams'];
            ?>
            <option><?php echo $row['streams']; ?></option>
            ?>
        <?php
        }
        mysql_data_seek($result, 0); // seeking record to beginning
        echo "</select><br><br>";
    }
    
    mysql_close($con);
    ?>
    </body>
</html>

sample table is as follows :

mysql> select * from app_pref;
+----+------------------------+
| id | streams                |
+----+------------------------+
|  1 | Computer Science       |
|  2 | Electronic Engineering |
|  3 | Physics                |
|  4 | Chemistry              |
+----+------------------------+
4 rows in set (0.00 sec)

Output