0

i am new to php and i try to solve this many times but i couldn't. this is my php code Can someone help me with this it should be easy for a php expert.

<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
      <head>
        <title>Highscores</title>
        </head>
    <body>
                <table border='1'>
         <tr>
          <th>user</th>
          <th>score</th>
          <th>Date</th>
         </tr>
            ";  
             while ($name = mysql_fetch_array($username) )  
              {
                  echo "<tr>
                        <td>" . $name ['username'] . "</td>";            
              }    
             while( $row = mysql_fetch_array($result))
              {      
                  echo"
                  <td>" . $row ['score'] . "</td>
                  <td>" . $row ['date'] . "</td>
                  </tr>";
              }
  echo"
     </table>
    </body>
   </html>
 ";

the table i want to take

2
  • 1
    Instead of executing 2 queries try and see if you can do with one using a join Commented Jan 12, 2014 at 15:54
  • please try to describe, where your challenge is and show what you already tried to solve it. Also describe, what is the result you want to achieve. For somebody that wants to help it is always hard to first figure out the question by following external links ;-) Commented Jan 12, 2014 at 16:08

4 Answers 4

1

mysql_* is deprecated! use mysqli_*

<?php
require_once 'connector.php';

$SQL = "SELECT u.username, h.score, h.date 
        FROM users AS u
        JOIN highscores AS h ON (h.user_id = u.users_id)";

$result = mysql_query($SQL) or die( mysql_error() );

echo "<html>
      <head>
        <title>Highscores</title>
      </head>
      <body>";


if( mysql_num_rows($result) > 0 )
{
    echo "<table border='1'>
          <tr>
          <th>user</th>
          <th>score</th>
          <th>Date</th>
      </tr>";

    while ( $row = mysql_fetch_array($result) )  
    {
        echo "<tr>";    

        printf("<td>%s</td>", $row['username']);
        printf("<td>%s</td>", $row['score']);
        printf("<td>%s</td>", $row['date']);

        echo "</tr>";   
    }

echo "</table>
      </body>
      </html>";
}

mysql_free_result($result);
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try this,

    $result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u  where hs.user_id=u.id ORDER BY score DESC');
    echo "<html>
          <head>
            <title>Highscores</title>
            </head>
        <body>
                    <table border='1'>
             <tr>
              <th>user</th>
              <th>score</th>
              <th>Date</th>
             </tr>
                "; 

         while( $row = mysql_fetch_array($result))
          {      
              echo"<tr>
              <td>" . $row ['username'] . "</td>   
              <td>" . $row ['score'] . "</td>
              <td>" . $row ['date'] . "</td>
              </tr>";
          }

Comments

0

Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this

echo"<html>
  <head>
    <title>Highscores</title>
    </head>
<body>
            <table border='1'>
     <tr>
      <th>user</th>
      <th>score</th>
      <th>Date</th>
     </tr>
        ";  

You can write like this:

 echo "<html>" .
      "<head>" .
      "<title>Highscores</title>"
      "</head>" .
      "<body>" .
      "<table border='1'>" .
      "<tr>" .
      "<th>user</th>" .
      "<th>score</th>" .
      "<th>Date</th>" .
      "</tr>" ;

The following code block

echo "<tr>
       <td>" . $name ['username'] . "</td>";

Should be written as

echo "<tr>" .
     "<td>" . $name ['username'] . "</td>";

In this way the code looks more readable as well.

Comments

0

The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.

<?
  while ($name = mysql_fetch_array($username)) {
    echo "<tr>";
    echo "<td>" . $name["username"] . "</td>";

    while ($row = mysql_fetch_array($result)) {
      echo "<td>" . $row["score"] . "</td>";
      echo "<td>" . $row["date"] . "</td>";
    }

    echo "</tr>";
  }
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.