For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > November 2004 > Using a loop on a result destroys array??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using a loop on a result destroys array??
Mark Benson

2004-11-24, 8:55 am

I have another issue. I have a fairly large script that requires data to be read for 2 separate purposes from the results of the same query. e.g.:

========================================
=================

<?php

$phish = $_GET["phish"];

$result = mysql_query("SELECT foo, bar, steak FROM footable WHERE foo LIKE '%$phish%'");

// loop *1*
where (mysql_fetch_array($result) as $row1) {
$crab = $row1["bar"];
}

print $crab;

?>

<LOTS OF HTML>

<?php

// loop *2*

$array_stuff = array();
$line = 0;

where (mysql_fetch_array($result) as $row1) {
$array_stuff[$line] = array()
$array_stuff[$line]["foo"] = $row1["foo"];
$array_stuff[$line]["bar"] = print $row1["bar"];
$array_stuff[$line]["steak"] = print $row1["steak"];
}

print_r $array_stuff;

?>

========================================
=============

From the above code, loop 2 returns a blank array, the output is simply "Array ()". If I remove the loop (by commentiung it out) that extracts $crab the mysql $result array is suddenly full of the correct values. It seems to me that loop 1 is destroying o
r somehow not resetting $result. Very odd...

--
Mark Benson

http://homepage.mac.com/markbenson
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com