ok I have a simple question, I think

When I create a query and assign the results to a variable, I can only get the data out of the variable 1 time, when I try to call the variable again the variable is empty

///////////////////////////Example Script

$qry = "SELECT *
FROM table_tester
WHERE city = 'sfo'
";

$result = mysql_query($qry_result, $db);

while ($row=mysql_fetch_array($result))
{
$row[city] = $city[];
}

while ($row2=mysql_fetch_array($result))
{
$row2[city] = $city2[];
}

///////////////////////////End Example Script

when I run this script $row2/$city2 return no results

How do I fix this?

Thanks for your help

    Hi webaholic,

    Assuming that you are just testing, here's one straightforward way ...

    <html> 
    <head> 
    <title>Test</title> 
    </head><?php 
    $qry = "
    	SELECT * 
    	FROM table_tester 
    	WHERE city = 'sfo' 
    "; 
    
    $result = mysql_query($qry, $db); 
    while($row = mysql_fetch_array($result)){ 
    	$city[] = $row['city']; 
    } 
    
    // Run the query again!!
    // Use the same variable names as before.
    $result = mysql_query($qry, $db); 
    while($row = mysql_fetch_array($result)){ 
    	$city2[] = $row['city']; 
    } 
    echo '<hr><strong>city</strong>: <pre>';	print_r($city);	echo '</pre>';
    echo '<hr><strong>city2</strong>: <pre>';	print_r($city2);	echo '</pre>';
    ?>
    </body> 
    </html>

    ... because, of course, the second loop is completely redundant 😉

    Paul.

      Write a Reply...