Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do I Generate Multiple query results in a loop

    I'm trying to do this
    Code:
    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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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/...ved-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
    Code:
    select * from table where `group` IN (1,2,3,4,5)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    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/...ved-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
    Code:
    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

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Let me try to elaborate what I intent to do...
    Code:
    <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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •