Use mysql_fetch_assoc more than once

1

I have a big question about mysql_fetch_assoc in while. what happens is the following, I have a page where I do a database search and return for a while while the existing information with mysql_fetch_assoc, the problem is, when I want to do this while twice, it simply aborts, so I'm doing two you search the database on the same page, only changing one situation because of the first mysql_fetch_assoc that overrides the second and so on. the code acts is this.

$buscar_conteudotag = mysql_query('SELECT msg_message FROM flq_message WHERE msg_private LIKE 0 ORDER BY msg_id DESC') or die (mysql_error());
while($quantidade_de_tag = mysql_fetch_assoc($buscar_conteudotag)) {}

$buscar_conteudoclosest = mysql_query("SELECT msg_message FROM flq_message WHERE msg_private LIKE 0 AND msg_message LIKE '%".$closest."%' OR '%".$closest."%' ORDER BY msg_id DESC") or die (mysql_error());
while($buscar_conteudolinhaclosest = mysql_fetch_assoc($buscar_conteudoclosest)){}

$buscar_conteudotag = mysql_query("SELECT msg_message FROM flq_message WHERE msg_private LIKE 0 AND msg_message LIKE '%".trim(substr($siteuri, 1))."%' OR '%".trim(substr($siteuri, 1))."%' ORDER BY msg_id DESC") or die (mysql_error());
while($quantidade_de_tag = mysql_fetch_assoc($buscar_conteudotag)) {}

What I look for in case is a way to perform only one SELECT for all three whiles. Does it exist? if anyone can help me in this factor.

    
asked by anonymous 31.07.2014 / 23:57

2 answers

3

What happens is that mysql_fetch_assoc "consumes" the lines, as it says in the PHP manual: link

  

Returns an associative array that matches the line obtained and moves the   internal pointer of the data ahead.

What you can do is save the result to an array ...

$conteudotag = array();
$buscar_conteudotag = mysql_query('SELECT msg_message FROM ...') or die (mysql_error());
while ($quantidade_de_tag = mysql_fetch_assoc($buscar_conteudotag)) {
    $conteudotag[] = quantidade_de_tag;
}

... and then use it wherever you need it.

    
01.08.2014 / 02:33
2

As you say in the description, it looks like the first search already gives you all the results you need, and your only problem is that mysql_fetch_assoc is moving the internal pointer to the end of the data during the while.

If this is the case, you can move the pointer back to the first line of the result using mysql_data_seek before doing the next while, for example:

mysql_data_seek($buscar_conteudotag, 0)
    
03.08.2014 / 06:16