Hello,

Here is a snip of my code and although the mysql connection work and even the $num calcullate the numrows work as the tables gets created unfortunately no data gets populated, can any one one advise.

1 - mysql_connect (OK)
2 - mysql_select_db (OK)
3 - mysql_numrows (OK)
4 - table gets created based on $max even but no data get populated, please help.

All this goes inside a html page!

  include 'config.php';

                                mysql_connect($hostname,$username,$password);
                                @mysql_select_db($database) or die('Could not connect: ' . mysql_error());
                                $query="SELECT name,correct,wrong,percentage,level,date FROM stats ORDER BY correct DESC LIMIT $max";

                                if (!$query) {
                                            die('Invalid query: ' . mysql_error());
                                }           

                                $result=mysql_query($query);

                                $num=mysql_numrows($result);

                                ?>
                                <table border="1" cellspacing="2" cellpadding="2">
                                <tr>
                                <td><font face="Arial, Helvetica, sans-serif">Name</font></td>
                                <td><font face="Arial, Helvetica, sans-serif">Correct</font></td>
                                <td><font face="Arial, Helvetica, sans-serif">Wrong</font></td>
                                <td><font face="Arial, Helvetica, sans-serif">Percent</font></td>
                                <td><font face="Arial, Helvetica, sans-serif">Level</font></td>
                                <td><font face="Arial, Helvetica, sans-serif">Date</font></td>
                                </tr>

                                <?php
                                $i=0;
                                while ($i < $num) {

                                $f1=mysql_result($result,$i,"field1");
                                $f2=mysql_result($result,$i,"field2");
                                $f3=mysql_result($result,$i,"field3");
                                $f4=mysql_result($result,$i,"field4");
                                $f5=mysql_result($result,$i,"field5");
                                $f6=mysql_result($result,$i,"field6");

                                ?>

                                <tr>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
                                <td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
                                </tr>

                                <?php
                                        $i++;
                                }
                                mysql_close();
                                ?>

$num=mysql_numrows($result) is wrong, it should be:
mysql_num_rows($result);

How strange that the script tests that $query exists (which is a string that is defined the previous line), then sends the query to the DB without any error checking:
$result=mysql_query($query);

Hi Adam,

Thank you for your reply and advice, I have corrected the spelling mistake mysql_num_rows, I still need to assign it to a varible to $num as I use it in the loop while

($i < $num) {}

Main issue still remains where the output of the query is not being assigned to my varibles and populating the fileds.

I still looking ofr additional help :-)

Yes, you still need to assign the num_rows to a variable:
$num=mysql_num_rows($result)
Did you assign it to $num? What happened?

The $num returns the correct number of rows, that does not seem to be the problem, the issue seems to me that I am just not getting any fields / columns from my query and i don't understand as it all looks perfectly fine.

I'm not familiar with this syntax:
$f1=mysql_result($result,$i,"field1");
I would do it a bit differently:

<!--use a table class rather than repeat font face declaration every line-->
<table class="table" border="1" cellspacing="2" cellpadding="2">
<tr><td>Name</td>
<td>Correct</td>
<td>Wrong</td>
<td>Percent</td>
<td>Level</td>
<td>Date</td></tr>

<?php 
// You don't need to run a while loop depending on num_results,
// just use while(mysql_fetch_array), will stop when no more results.

$sql = "SELECT * FROM tablename";
if(!$result = mysql_query($sql)){// put all DB operations inside error check loop
    die('Invalid query: ' . mysql_error());
}
if(!mysql_num_rows($result) > 0){// using num_rows to make sure there are results to display.
    die("no results, aborting...");
    } 

while($row = mysql_fetch_assoc($result)){
    echo "<tr><td>".$row['name']."</td>"; //<tr> on the first row
    echo "<td>".$row['correct']."</td>";
    echo "<td>".$row['wrong']."</td>";
    echo "<td>".$row['percentage']."</td>";
    echo "<td>".$row['level']."</td>";
    echo "<td>".$row['date']."</td></tr>"; //close </tr> on last row.
}
?>

My Man, you are spot on, this worked as a treat and does exacly what I wanted. I still don't know why my loop did not work :-P, anyway, many thanks for all your help

Hey that's great :)
mysql() is dead, long live mysqli and pdo
Can you mark this thread as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.