0

Yesterday I wrote up some javascript/php to retrieve info from a table named 'users' in a MySQL database. It takes the username and their 'wealth' and ranks them on score. But, the code below shows nothing except the headers. What's wrong with it? Thanks.

<div id="board">
    <table border="1" cellspacing="0" cellpadding="2" width="620"><tbody>
            <thead>
            <tr>
                <td>Username</td>
                <td>Clicks</td>                  
            </tr>
        </thead>
        <tbody>
            <?php

            $con = mysqli_connect('localhost','xxxx','xxxx','xxxx');

            if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
            } else {

            mysql_select_db("users");
            $results = mysql_query("SELECT username, wealth FROM users ORDER BY wealth DESC LIMIT 10");

            while($row = mysql_fetch_array($results)) {
            $username = $row['username'];
            $wealth = $row['wealth']; } 
            }
            ?>
            <tr>
                <td><?php echo $username;?></td>
                <td><?php echo $wealth;?></td>
            </tr>
        <?php               
            mysqli_close($con);            
            ?>
    </tbody>
</table>

Caspar
  • 169
  • 2
  • 9
  • You need to echo the rows out in your while loop. – Dan Weber Jan 27 '17 at 20:43
  • One issue is that the table rows that would show your query results are outside the fetch loop. But it seems like you'd still at least see the last one. – Don't Panic Jan 27 '17 at 20:43
  • Oh, wait, no you wouldn't. You're mixing mysql with mysqli. – Don't Panic Jan 27 '17 at 20:46
  • Is that where I've tripped up? @Don'tPanic – Caspar Jan 27 '17 at 20:51
  • Yeah, I'd fix that first. The other thing with the loop will be easier to fix. But once you've connected with `mysqli_connect`, you have to use all `mysqli` functions rather than `mysql` ones. Depending on your PHP version, those might not even exist, but even in older versions where before they were removed, you can't mix the two. – Don't Panic Jan 27 '17 at 20:59
  • See this Q&A: http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php – Don't Panic Jan 27 '17 at 20:59

1 Answers1

-1

You need to echo rows out in your while loop.

<div id="board">
    <table border="1" cellspacing="0" cellpadding="2" width="620"><tbody>
            <thead>
            <tr>
                <td>Username</td>
                <td>Clicks</td>                  
            </tr>
        </thead>
        <tbody>
            <?php

            $con = mysqli_connect('localhost','xxxx','xxxx','xxxx');

            if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
            } else {

            mysql_select_db("users");
            $results = mysql_query("SELECT username, wealth FROM users ORDER BY wealth DESC LIMIT 10");

            while($row = mysql_fetch_array($results)) {
               $username = $row['username'];
               $wealth = $row['wealth']; 

               echo "<tr><td>$username</td><td>$wealth</td></tr>";
            }
          
            mysqli_close($con);  

            echo "<script> var results = $results; console.log(results);</script>";          
            ?>
    </tbody>
</table>
Dan Weber
  • 1,227
  • 1
  • 11
  • 22