Search code examples
phpmysqlhtmlcodeigniterfavorites

Get all favorites for current user


I've got a table in the database called "favorites" with 3 columns (user_id, bookmarked_song_id, bookmark_tag) and I want to get all the Bookmarked_song_id for the current user.

 $username = $this->session->userdata('username');
 $uidq = mysql_query('SELECT user_id FROM users WHERE username="' . $username . '"');
 $rq = mysql_fetch_assoc($uidq);
 $user_id = $rq['user_id'];
 $getfavq = mysql_query("SELECT * FROM favorites WHERE user_id=$user_id");
 $favsr = mysql_fetch_array($getfavq); //contains all the information from the favorites database where user_id is the user_of the currently logged-in user

And I don't know what to use next... I want to have something like:

foreach($favsr['bookmarked_song_id'] as $song_id) {
$getsongq = mysql_query("SELECT * FROM songs WHERE song_id=$song_id");
$getsongr = mysql_fetch_assoc($getsongq);
$singer = $getsongr['singer'];
$song_name = $getsongr['song_name'];}

Obviously the method is wrong because I get: "Invalid argument supplied for foreach()". Can anyone help me with getting the songs? Thanks in advance.


Solution

  • It should be this:

    $favsr = mysql_fetch_array($getfavq, MYSQL_ASSOC);
    foreach($favsr as $row) {
        $songid = $row['bookmarked_song_id'];
         ...
    }