简体   繁体   English

使用第二个表(PHP)的结果填充SQL空数据库值

[英]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. 我创建了一个数据库,该数据库使用来自plex服务器的xml提要自动更新,但是有一整夜困扰着我。

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). 我正在尝试为最近添加的内容创建一个滑块,但是当涉及到“摘要”时,电影会将这些摘要添加到我的“最近”表中,而在电视剧集中则没有(不知道为什么plex的xml会这样,但是我试图解决它)。 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) 我想做的是遍历最近的表,如果“ recent”->“ Desc”字段为空,请从“ episodes”->“ Desc”中拉取内容(按显示标题进行匹配)

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) 我会尽力澄清所有不清楚的地方(很晚了,您知道: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: 然后,您可以完全摆脱第二个select语句,而while循环中的PHP代码将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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