php - Multiple MySQL Inputs -


i have script looks retrieves song information people scrobbling on lastfm:

class nowplaying{      private $url;     private $notrackplayingmessage;      function __construct($user, $api_key){          // construct url         $this->url  = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1';         $this->url .= '&user=' . $user . '&api_key=' . $api_key;          // default message         $this->notrackplayingmessage = 'nothing playing right now!';     }      // return artist , track playing     public function getnowplaying(){          // create xml object         $xml = simplexml_load_file($this->url);          // latest track         $track = $xml->recenttracks->track;          // check if track playing          $nowplaying = $track->attributes()->nowplaying;          // return track , artist if music playing, otherwise show message         if($nowplaying){             $artist = $track->artist;             $songname = $track->name;             return $artist . ' - ' . $songname;         }         else{             return $this->notrackplayingmessage;         }     }      // set message shown when no music playing     public function setnotrackplayingmessage($messagein){         $this->notrackplayingmessage = $messagein;     }  } // end class  $nowplaying = new nowplaying($id, 'apigoeshere'); $nowplaying->setnotrackplayingmessage($id); // optional $currentplaying = $nowplaying->getnowplaying(); 

while useful individual lastfm account want run several accounts through script details stored in mysql database. table has 2 columns, lastfmusername , currentsong. want find songs lastfm user's listening , store them in currentsong field.

i've tried adding following top:

$sql = "select lastfmusername data"; $id = $db->query($sql); 

then following bottom:

$sql2 = "update lastfmtable set currentsong = '$currentplaying' lastfmusername = '$id'"; $cursong = $db->query($sql2); 

but failed i'm not sure how approach this. appreciated.

$sql = "select lastfmusername data"; 

will return array containing values of lastfmusername, not one.

try instead:

$sql = "select lastfmusername data"; $users = $db->query($sql); $id = $users[0]['lastfmusername']; 

meaning: $id store first result.

you'll need loop through result of users , run update query each user. trying should this:

foreach($users $r){    $id= $r['lastfmusername'];    $nowplaying = new nowplaying($id, 'apigoeshere');    $nowplaying->setnotrackplayingmessage($id); // optional    $currentplaying = $nowplaying->getnowplaying();    $sql2 = "update lastfmtable set currentsong = '$currentplaying' lastfmusername = '$id'";    $cursong = $db->query($sql2); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -