I have a relational database with two tables. One table contains titles of musical pieces called ‘works’ (and some other info about those pieces), and the other table contains urls to mp3 tracks of those musical pieces called ‘tracks’. I’ve been trying to get this simple php code to display the url when a url is available for that musical piece. But, all my attempts using if statements and “empty” function to check whether a url is available in the database, turns out always as having a url (even for those pieces that don’t have a url). I also tried !$row2 with the same issue. I’ve simplified the code in order to test if this works (see code below). I appreciate if you could highlight the novice mistakes I made.
$row retrieves an array relating to the first table (work_title, etc.) $row2 retrieves an array relating to the second table (urls, etc.) The SQL is confirmed to be working well since I already got all the details (from the two tables) into the page.
/*********** Getting 'works' data and displaying *********/
$sql = "select work_title, alternate_titles from works where composer_id='$compid'";
$query2 = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query2);
$sql2 = "select track_url from tracks INNER JOIN works ON tracks.work_id=works.work_id";
$query3 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_array($query3);
if(!$row)
{
echo "No musical pieces found.";
}
while ($row = mysqli_fetch_array($query2))
{
if(empty($row2)){
echo $row['work_title'] . " - no url" . "<br />";}
else{
echo $row['work_title'] . " - url available" . "<br />";}
}