I’m trying to insert the image urls from the last.fm api into my MySQL database. I’m able to access the correct url data from the api, iterate through and echo it out but when I try to add it urls to my MySQL database, the UPDATE statement seems to break the while loop after the first iteration.
Any help would be appreciated. Thanks in advance.
I’m getting the follow error message when I run the page
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in C:\xampp\htdocs\musicfly\runonce_albumart.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\musicfly\runonce_albumart.php on line 20
include("dbconnect.php");
$read = "
SELECT artist.artist_name, album.album_name, album.rank, album.year
FROM album
INNER JOIN
artist ON artist.artist_id = album.FK_artist_id
GROUP BY album.rank";
$result = $conn -> query($read);
if(!$result){
echo $conn -> error;
}
while ($row = $result->fetch_assoc()){
$album_name=$row['album_name'];
$artist_name=$row['artist_name'];
$endpoint = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=APIKEYREMOVED&artist={$artist_name}&album={$album_name}&format=json";
$endpoint = str_replace(' ', '%20', $endpoint);
$endpoint = str_replace("'", '%27', $endpoint);
$jsonstring = file_get_contents($endpoint);
$arraydata = json_decode($jsonstring, true);
$album = $arraydata['album'];
$artist = $album['artist'];
$album_name = $album['name'];
$image_url = $album['image'][4]['#text'];
echo "<p>{$artist}</p>";
echo "<p>$album_name;</p>";
echo "<p>$image_url</p>";
$album_name = addslashes($album_name);
$insertquery = "
UPDATE
album
SET
img_url = '$image_url'
WHERE
album_album ='$album_name'
";
$result = $conn -> query($insertquery);
if(!$result){
echo $conn -> error;
}
}
>Solution :
You’re using the same variable name $result for your UPDATE query. Simply choose a different variable name.
$result2 = $conn -> query($insertquery);
if(!$result2){
echo $conn -> error;
}