简体   繁体   English

在特定表列上显示PHP在MySQL上的行内容(不同表上的.row名称相同)

[英]Show MySQL row content on PHP from specific table column (same .row name on different tables)

In my app, I have two tables that contain two columns with the same column name 在我的应用程序中,我有两个包含两个具有相同列名的 列的表

Table TITLES 表标题

COLUMN = name    - CONTENT example: Jurassic World Trailer

Table VIDEOS 表视频

COLUMN = name    - CONTENT example:<img src='/abc.png'>

I need to display rows from both columns "name" in my PHP file, but the row="name" from TITLES table always comes first, and I cant dispaly the image from the VIDEOS row="name". 我需要在PHP文件中显示“名称”两列中的行,但是TITLES表中的row =“ name”总是最先出现,并且我无法从VIDEOS row =“ name”中显示图像。

        // output data of each row
    while($row = $result->fetch_assoc()) {      

            $written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td align="right"><div class="44"> '.$row["name"];

It is clear that I must change the way in which the second query is extracted for the table "videos" and add besides $row['name'] and the name of the table , but how should I do this? 显然,我必须更改为“ videos”表提取第二个查询的方式,并在$ row ['name'] 和表名之外添加,但是我应该怎么做?

<div class="44"> '$VIDEOS-TABLE->.$row["name"];

Expected result for the code above 上面代码的预期结果

target="_parent">Jurassic World Trailer</a> //name come from titles table and is OK
</b></div></td></tr><tr><td align="right">
<div class="44"><img src='/abc.png'> //name NEED to come from VIDEOS table

Thanks for the help. 谢谢您的帮助。

UPDATE - more code. 更新-更多代码。

// First query
$sql = "SELECT MAX(l.`id`) FROM `videos` as l WHERE l.approved = 1" . $type2 ." GROUP BY l.`title_id`,  l.`name`, l.`season`, l.`episode` ORDER BY l.`id` DESC LIMIT 1000";
$result=mysqli_query($conn,$sql);
if ($result->num_rows > 0){
    $ids_max=[];
    while($row = $result->fetch_row())
        $ids_max[]=$row[0];
    $where_in = implode(',', $ids_max); 

    // Second query: 
    $sql = "SELECT l.id, l.name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;
    //print $sql;
    $result = $last_id = $conn->query($sql);

    //write head
    $file = fopen($filename, "w");
    $written_head = "
        <head>
        <link type=\"text/css\" rel=\"stylesheet\" href=\"/styles.css\"> 
        </head>
        <body>
    fwrite($file, $written_head . PHP_EOL);

    if ($result->num_rows > 0){
        // output data of each row
        while($row = $result->fetch_assoc()) {      
            if (empty($row['episode'])) {
                $written_fichas = "<div><table><tr><td><a href=/".$row['title_id'].'-'.$row['name'];
                $written_fichas = $written_fichas .' target="_parent"><img src='.$row['poster'].' /></a><div>&nbsp;</div></td>';
                $written_fichas = $written_fichas ."</tr><tr><td><table><tr><td></td></tr><tr><td><div class='55'><b><a href=/".$row['title_id'];
                $written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td><div class="44"> '.$row["name"];
                $written_fichas = $written_fichas .'</div></td></tr></table></td></tr></table></div>';

            fwrite($file, $written_fichas . PHP_EOL);
        }
    }else {
        fwrite($file, "0 results". PHP_EOL);}

Use this syntax in MySql: 在MySql中使用以下语法:

SELECT column_name AS alias_name FROM table_name;

SELECT column_name(s) FROM table_name AS alias_name;

Why would you use "AS" when aliasing a SQL table? 为什么给SQL表加上别名会使用“ AS”?

The following code is a solution for you: 以下代码为您提供了一个解决方案:

Update second query to this one: 将第二个查询更新为此:

$sql = "SELECT l.id, l.name as video_name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;

Update last line to this one: 将最后一行更新为这一行:

<div class="44"> '.$row["video_name"];

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

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