I'm trying to connect a select random to display information from tables 'tracks', 'albums'
select tracks, albums, artists, track.title, artist.name, album.title
From albums, tracks , artists
where albums.artistID = tracks.artistID;
and albums.artistID = artists.ID;
I'm not sure why it won't work and I'm trying to understand why, any help would be appreciated
Here is the piece of coding for the random, I'm unsure how to connect this with the 5 generated songs, along with their information displayed.
$query = "INSERT INTO songsoftheweek
SELECT * FROM tracks
ORDER BY RAND()
LIMIT 5";
$dbResult=mysql_query($query,$db);
For starters, this is one problem: select tracks, albums, artists
. The select
statement is for identifying the columns you want to select. The from
statement is for identifying tables...
There may be other things going on here... for instance, I don't see anything that looks at all like it's introducing randomness. Where is that supposed to be happening? But at least this should help solve one problem.
Give each track an ID field. Make a query which selects all fields from album, make which counts the rows.. and then put a rand() function behind that, example:
$con = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE");
$query = mysqli_query($con, "SELECT * FROM DATABASE;");
$count = mysqli_num_rows($query);
$random = rand(1,$count);
$trackquery = mysqli_query($con,"SELECT * FROM DATABASE WHERE ID = $random;");
while($row = mysqli_fetch_array($trackquery))
{
// do stuff
}