I’m trying to grab 10 results from a table that has 16k rows.
With in this table is a row called views that gets a +1 each time an artist is viewed.
But I’m getting unexpected results only the top artist.
I have the views row indexed for speed.
I know I have to loop it, but I’m unsure how to get all 10 rows
I haven’t dealt with looping or getting more than one row at a time and need help formatting it with the new mysqli
example here with printout of array returned
// Get Top Viewed Artist
$TopViewedArtist = mysql_query("SELECT * FROM `artists` ORDER BY `artists`.`views` DESC LIMIT 10");
$TopViewedArtistInfo = mysql_fetch_assoc($TopViewedArtist);
$TopViewedArtist_TotalRows = mysql_num_rows($TopViewedArtist);
print_r($TopViewedArtistInfo); // the data returned from the query
This is the solution to display the results of the artist name in a readable format.
$TopViewedArtists = mysql_query('SELECT * FROM `artists` ORDER BY `artists`.`views` DESC LIMIT 10');
while (($TopArtist = mysql_fetch_assoc($TopViewedArtists)) !== FALSE) {
//print_r($TopArtist);
echo $TopArtist['artist']; echo "<br>";
}
This code can be change for others. but needs to be updated to mysqli
mysql_queryreturns aresultresource object. Think of it as an array. The only way to read the contents of that array is to iterate through it. Think ofmysql_fetch_assocas the same aseachfor arrays: it returns the next row and increments the internal pointer. I think this is what you want to do:Keep in mind also that
mysql_fetch_assocreturns an array with multiple values. The array is supposed to contain everything you see; access the values with$artist['artist'](outputsGwen Stefani).May I suggest that you look into mysqli or PDO instead of the basic mysql functions? The only reason to use
mysql_functions is if you’re stuck with PHP 4 (no longer supported so nobody should still be using it) or old applications. This looks like neither, and as it also seems you have no existing experience with the interface you really ought to look into the better options.