Re-using a mysql_query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Re-using a mysql_query

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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 
?>
Post Reply