...

View Full Version : How do I Generate Multiple query results in a loop



jerry4all
07-02-2012, 08:49 AM
I'm trying to do this


for($i=1;$i<6;$++){
$query=mysql_query("select * from table where group='$i'");
}
I want to av store each query instance result to be able to fetch each where appropriate...
Any useful help is well appreciated

abduraooft
07-02-2012, 11:18 AM
You should make proper error checks to your query. In your case, group is a reserved keyword and thus you need to use backticks (``) to wrap it when using in the queries as the name of table/field. It's a not a good practice to use such words for this propose though, see http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.

To add error checks, you may change your query like
$query=mysql_query("select * from table where group='$i'") or die(mysql_error());

PS: Executing queries in loops would make your code ineffective. You may make an array/list of groups to form a query like

select * from table where `group` IN (1,2,3,4,5)

jerry4all
07-02-2012, 01:36 PM
You should make proper error checks to your query. In your case, group is a reserved keyword and thus you need to use backticks (``) to wrap it when using in the queries as the name of table/field. It's a not a good practice to use such words for this propose though, see http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.

To add error checks, you may change your query like
$query=mysql_query("select * from table where group='$i'") or die(mysql_error());

PS: Executing queries in loops would make your code ineffective. You may make an array/list of groups to form a query like

select * from table where `group` IN (1,2,3,4,5)

thanks @abduraooft...I'm actually just using random words to spell out what I want to do, won't use such word is real design.

Also ur suggestion will search all occurence of each of the arrary items from the table able give one arrary of result.
What I actually want to achieve is different $query result for each query using each count element....that is, something like...

[code]
$query1 = mysql_query("select * from table where grp=1");
$query2 = mysql_quer("select * from table where grp=2");
[code/]
...etc
thanks

jerry4all
07-02-2012, 02:01 PM
Let me try to elaborate what I intent to do...


<ul class="table_group">
<li><a href="#">Group A</a>
<table><tr>
<th align="left">Team</th>
<th>P</th>
<th>W</th>
<th>D</th>
<th>L</th>
<th>Pts</th>
<th>GD</th>
</tr>
<tr>
<th align="left">Nigeria</th>
<td>3</td>
<td>2</td>
<td>0</td>
<td>1</td>
<td>6</td>
<td>+6</td>
</tr>
<tr>
<th align="left">France</th>
<td>3</td>
<td>3</td>
<td>0</td>
<td>0</td>
<td>9</td>
<td>+3</td>
</tr>
<tr>
<th align="left">Holland</th>
<td>3</td>
<td>2</td>
<td>0</td>
<td>1</td>
<td>6</td>
<td>+3</td>
</tr>
<tr>
<th align="left">Argentina</th>
<td>3</td>
<td>2</td>
<td>0</td>
<td>1</td>
<td>6</td>
<td>+3</td>
</tr>
</table>
</li>
<li><a href="#">Group B</a></li>
<li><a href="#">Group C</a></li>
<li><a href="#">Group D</a></li>
</ul>


I'm trying to achieve this dynamically, with the possibility of having a dynamic number of groups which serves as the limiting variable in the forloop conditional statement.
Hope I've been able to make it clearer...Any help will be appreciated.

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum