简体   繁体   中英

fill SQL empty database value with result from second table (PHP)

Ok, so i'm not sure if i'm going about this a completely horrid way. However, i'm basically trying to create a movie database for my room mates to show what content we have on our server and to easily check this out.

I have created a database that is automatically updated using the xml feed from our plex server, however there is one small bit that has been bugging me all night.

I'm trying to create a slider for recently added content, but when it comes to a "summary", films add there summaries to my "recent" table, where as tv episodes do not (not sure why plex's xml goes like this but im trying to work around it). the TV episodes have there summary in the "episodes" table.

What i'm trying to do is loop through the recent table and if the "recent"->"Desc" field is empty, pull content from "episodes"->"Desc" instead (matching by show title)

The code I have for this loop so far is:

<?php
$con=mysqli_connect("localhost","","","plex");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

                $recent = mysqli_query($con,"SELECT * FROM recent LIMIT 0, 6");
                $shows = mysqli_query($con,"SELECT * FROM shows");
                while($row = mysqli_fetch_array($recent))
                {
                    $art = $row['Art'];
                    $title = $row['Title'];
                    if (empty($row['Desc'])){
                        /* STUCK HERE*/
                    }
                    else {
                        $summary = $row['Desc'];
                    }
                    echo "<img src='$art.jpg' alt='$summary' title='$title' />";
                }
                mysqli_close($con);
        ?>

And my tables look like:

**********episodes**********************      **********recent*************
****************************************      *****************************
*id**eptitle**showtitle**desc*thumb*art*      *id**Title**Desc**thumb**art*
*  **       **         **    *     *   *      *  **     **    **     **   *

I will do my best to clarify on anything that isn't clear (it's late, you know :P)

If you'd like to do this with a single query here is what you can do:

$recent = mysqli_query($con,"SELECT r.Art, r.Title, if(r.Desc is null or r.Desc = '', e.Desc, r.Desc) as `Desc` FROM recent r inner join episodes e on r.Title = e.showtitle LIMIT 0, 6");

You could then get rid of the second select statement altogether, and you PHP code within the while loop would be:

while($row = mysqli_fetch_array($recent))
{
    $art = $row['Art'];
    $title = $row['Title'];
    $summary = $row['Desc'];
    echo "<img src='$art.jpg' alt='$summary' title='$title' />";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM