dongzhang8680 2019-02-17 13:40
浏览 53
已采纳

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

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".

        // 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?

<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);}
  • 写回答

1条回答 默认 最新

  • duandi5328 2019-02-17 13:50
    关注

    Use this syntax in 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?

    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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备