Use mysql_fetch_assoc more than once

Asked

Viewed 1,371 times

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 to a while existing information with mysql_fetch_assoc, the problem is, when I want to do this while twice, it simply overrides, so I’m doing two database searches 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’m looking for in this case is a way to perform only one SELECT for all three whiles. is there? if anyone can help me in this factor.

  • 1

    A tip: the functions mysql_ are obsolete. Try using Mysqli.

  • @Lucas understand, but I’m learning yet, I haven’t searched much on sqli, but it works on any linux server that runs slq normal?

  • According to the manual, you must have Mysql >= 4.1.13 and PHP >= 5.0.0

2 answers

3

From what you say in the description, it seems that the first search already gives you all the results you need, and your only problem is that mysql_fetch_assoc keeps changing the internal pointer until the end of the data during the while.

If so, there is the possibility of moving 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)
  • in case I want to be able to use while with the same results many times, but Assoc doesn’t allow me to do this, then I just need to insert Seek before another Assoc?

  • No, before another while. You tried?

3


What happens is that the mysql_fetch_assoc "consumes" lines, as it says in the PHP manual: http://php.net/manual/en/function.mysql-fetch-assoc.php

Returns an associative matrix that corresponds to the obtained line and moves the Data internal pointer 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 it takes.

  • in case I only do this once on the page, and while I’m using other while of the same content I put $conteudotag[]; in place of Assoc?

  • you can use foreach to iterate the array

Browser other questions tagged

You are not signed in. Login or sign up in order to post.