1

I have a database which looks like so -

Image 1

I am trying to fetch the top 10 entries based on time (entries with top 10 values in time column). I have the following code.

    <?php
    include_once("connect.php");
    $sql = "SELECT * FROM scores order by time desc limit 10";
    $query = mysql_query($sql) or die("systemResult=Error");
    $counter    = mysql_num_rows($query);

    if($counter>0)
    {
        print("systemResult=Success");
        $array = mysql_fetch_array($query);

        foreach($array as $data)
        {
            $athleteName    = $data["athleteName"];
            $email = $data["email"];
            $time = $data["time"];
            $timeStamp = $data["timeStamp"];
            $country = $data["country"];

            print "&athleteName=" . $athleteName;
            print "&email=" . $email;
            print "&time=".$time;
            print "&timeStamp=".$timeStamp;
            print "&country=".$country;
         }
    }
    else
    {
        print("systemResult=Error");
    }
?>

The output I am getting is

systemResult=Success&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=I&email=I&time=I&timeStamp=I&country=I&athleteName=I&email=I&time=I&timeStamp=I&country=I

As can be seen, the output I am getting is not what is on the table in database. I am getting wierd values. What am I doing wrong?

2 Answers 2

2

You don't need to use for each in your case, and if so, just print $data, try to remove foreach loop, and if you want to get all records, then, use while:

 while($data = mysql_fetch_array($query))
    {
        $athleteName    = $data["athleteName"];
        $email = $data["email"];
        $time = $data["time"];
        $timeStamp = $data["timeStamp"];
        $country = $data["country"];

        print "&athleteName=" . $athleteName;
        print "&email=" . $email;
        print "&time=".$time;
        print "&timeStamp=".$timeStamp;
        print "&country=".$country;
     }
Sign up to request clarification or add additional context in comments.

Comments

2

try

while($data = mysql_fetch_array($query)) {
    $athleteName    = $data["athleteName"];
    //...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.