Page 1 of 1

Re-using a mysql_query

Posted: Thu May 31, 2007 2:34 pm
by keveh
Hi There,

I have come across a slight problem whilst producing a page, well not so much a problem, more of an annoyance.

I have the following code:

Code: Select all

$tabSql = mysql_query("SELECT * FROM videoTabs WHERE tabCat = $catID", $link);
while($tabJava = mysql_fetch_array($tabSql)){
	echo "option[". $tabCount ."] = \"" . $tabJava[tabDivID] . "\""."\n";
	$tabCount++;
}
... some more code ....

Code: Select all

$tabSql = mysql_query("SELECT * FROM videoTabs WHERE tabCat = $catID", $link);
// generate tab buttons
while($tabBut = mysql_fetch_array($tabSql)){
	echo '<li><a href="#videoTabs" onclick="toggleTab(\''. $tabBut[tabDivID] .'\')"><span>'. strtoupper($tabBut[tabName]) .'</span></a></li>'."\n";
}
As you can see, I am having to call $tabSql twice.

If I get rid of the second $tabSql then the second whilst loop will not work.

Is there anyway to re-use that array that has been built using the original $tabSql query?

Posted: Thu May 31, 2007 2:45 pm
by RobertGonzalez

Code: Select all

<?php
$tabJava = array();
$sql = mysql_query("SELECT * FROM videoTabs WHERE tabCat = $catID", $link) or die('Crap.');

while ($row = mysql_fetch_array($sql)){
    $tabJava[] = $row;
}

// Now I have that query result in a  nice little array for whenever I want to use it 
?>