SEJBR
SEJBR

Reputation: 368

PHP: Two mysql_fetch_array in table

I'm creating a table with data from two queries and tried to connect them in a table. Tried using something like this :

$counter = 1; 

    echo '<table border="0"  cellpadding="0" cellspacing="3"><tr>
                <td width="10%">Miejsce</td>
                <td width="25%">Nick</td>
                <td width="20%">SteamID</td>
                <td width="20%">Punkty</td>
                </tr>';

                    while($row = mysql_fetch_array($skill_b) AND $row2 = mysql_fetch_array($sid_b))
                    {
                      echo '<tr class="select">';
                      echo '<td>'.$counter.'</td>';
                      echo '<td><center>'.$row['lastName'].'</center></td>';
                      echo '<td><center>'.$row2['uniqueId'].'</center></td>'; 
                      echo '<td><center>'.$row['skill'].'</center></td>';
                      echo '</tr>
                    $counter++; 
                    } 

    echo '</table>';

and

$counter = 1; 
    echo '<table border="0"  cellpadding="0" cellspacing="3"><tr>
                <td width="10%">Miejsce</td>
                <td width="25%">Nick</td>
                <td width="20%">SteamID</td>
                <td width="20%">Punkty</td>
                </tr>';
                                          echo '<tr class="select">';
                                          echo '<td>'.$counter.'</td>';
                    while($row = mysql_fetch_array($skill_b))
                    {
                      echo '<td>'.$row['lastName'].'</td>';
                    } 
                    while($row2 = mysql_fetch_array($sid_b))
                    {
                      echo '<td>'.$row2['uniqueId'].'</td>'; 
                    } 
                    while($row3 = mysql_fetch_array($skill_b))
                    {
                      echo '<td>'.$row3['skill'].'</td>';
                    } 
                    $counter++; 
    echo '</table>';

The second one almost works, although it crashes the whole table, so I can't see all the results.

Query looks like this if there's something that can be changed in them:

   $query_skill = sprintf(
  "SELECT skill, lastName
  From hlstats_Players
  ORDER BY skill DESC
  LIMIT 10");

$skill_b = mysql_query($query_skill);
$skill = mysql_result( $skill_b, -1 );

$query_sid = sprintf(
  "SELECT uniqueId
  From hlstats_PlayerUniqueIds
  WHERE playerId='$pid_b'"
  );

$sid_b = mysql_query($query_sid);
$sid = mysql_result( $sid_b, 0 );

Upvotes: 2

Views: 5918

Answers (2)

Arun_gok
Arun_gok

Reputation: 96

try this this works fine for me

<?php
mysql_connect('localhost','root','');
mysql_select_db('your_db');
$counter = 1; 
    echo '<table border="0" style="text-align:center;" cellpadding="0" cellspacing="3"><tr>
                <td width="10%">Miejsce</td>
                <td width="25%">Nick</td>
                <td width="20%">SteamID</td>
                <td width="20%">Punkty</td>
                </tr>';

$skill_b = mysql_query('select * from table_name limit 10');
$sid_b = mysql_query('select * from table_name limit 10');

                    while($row = mysql_fetch_array($skill_b) AND $row2 = mysql_fetch_array($sid_b))
                    {
                      echo '<tr class="select">';
                      echo '<td>'.$counter.'</td>';
                      echo '<td><center>'.$row['colu_name'].'</center></td>';
                      echo '<td><center>'.$row2['colu_name'].'</center></td>'; 
                      echo '<td><center>'.$row['colu_name'].'</center></td>';
                      echo '</tr>';
                    $counter++; 
                    } 

    echo '</table>';
    ?>

please comment if something goes wrong, if any mistake please mention it

Thank you

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Your first attempt is almost right. The only problem is the operator precedence.

Try this:

while(($row = mysql_fetch_array($skill_b)) && ($row2 = mysql_fetch_array($sid_b)))

That said, you should probably restructure your query using a JOIN, but I can't give any additional help there without seeing your current queries. See edit below.

Also, note that <center> is deprecated in favour of CSS. In this case:

<td style="text-align:center">

EDIT: Try this query:

SELECT a.skill, a.lastName, b.uniqueId
FROM hlstats_Players a
JOIN jlstats_PlayerUniqueIds b ON a.id = b.playerId
LIMIT 10

Upvotes: 1

Related Questions