user962206
user962206

Reputation: 16147

Retrieving multiple results from a query in PHP

I am creating a script where I need to retrieve several information in a database. I am expecting to receive these datas

enter image description here

I need to get those 2 track paths. but all I get is the first result and that's it. I've run my SQL script in phpmyadmin it is completely showing all of the datas that I needed. but when I execute that query in PHP. it only returns to me the first result. Here's my script

public static function find_user_tracks($id){
    global $mySQL;
    $sql = "SELECT * FROM `tracks` WHERE account_id = {$id}";
    $result_set =  $mySQL->query($sql);
    return $result_set->fetch_assoc();
} 

And here's the code I call to print the results

$row = Track::find_user_tracks($id);
                echo "{$row['track_path']}<br>";    

but all I receive is only the first path.

Upvotes: 0

Views: 177

Answers (1)

xdazz
xdazz

Reputation: 160963

You need to do a loop to return all records.

public static function find_user_tracks($id){
    global $mySQL;
    $sql = "SELECT * FROM `tracks` WHERE account_id = {$id}";
    $result_set =  $mySQL->query($sql);
    $ret = array();
    while($row = $result_set->fetch_assoc()) {
      $ret[] = $row;
    }
    return $ret;
} 

Upvotes: 2

Related Questions