dougu5950 2016-05-12 09:09
浏览 13

搜索MySQL并显示其他表的结果

I'm have made a search function in PHP for my MySQL database. The search works but I want to display the result in a special way. I'm using cards to show the tables where some information is from the table (LessonPlans) I search and another information is from another table (Images). The original code was this:

<?php
require_once("db_link.inc.php");

if(isset($_GET['search'])) {
    $search = $link->escape_string($_GET['search']);
    $query = $link->query("SELECT Subject, Level, Aim FROM LessonPlans WHERE Subject LIKE '%".$search."%' OR Level LIKE '%".$search."%' OR Aim LIKE '%".$search."%' AND Language='English'");

if($query->num_rows){
    while($r = $query->fetch_object()){

    echo '<div class="col s6 m4 l3">
            <div class="card">
                <div class="card-image waves-effect waves-block waves-light">
                    <img class="activator" src="../mlslp/assets/img/'. $r->Name-img .'" alt="'. $r->Alt-img .'">
                </div>
                <div class="card-content">
                    <span class="card-title activator grey-text text-darken-4 truncate tooltipped" data-position="bottom" data-delay="800" data-tooltip="'.$r->Subject.'">'.$r->Subject.'</span>
                    <p class="blue-text">'.$r->Level.'</p>
                </div>
                <div class="card-action">
                    <a href="lessonplan.php?Id='. $r->Id .'">Open Lesson Plan</a>
                </div>
                <div class="card-reveal">
                    <span class="card-title grey-text text-darken-4">'.$r->Subject.'<i class="material-icons right">close</i></span>
                    <p>'.$r->Aim.'</p>
                </div>
            </div>
        </div>';
        }
    } 
}
?>

The tables I want to use are:

LessonPlans
Id | ImageId | Subject | Level | Aim | Language

and

Image
ImageId | Name-img | Alt-img

As it is it shows the correct result from LessonPlans but not the information that is not searchable. I know it will not work like this and I've tried with UNION, JOIN and as well with just two SELECT queries. As soon as I add the Images table into the query I get this error message:

Notice: Trying to get property of non-object in ...

From what I understand, this is because $query is not an object when I add another table. What I don't understand is how I get around this. There must be a way to search one table but display results from two tables. Does anyone know how to do this?

What query do I need and how do I display it all?

  • 写回答

2条回答 默认 最新

  • dongpo5264 2016-05-12 09:19
    关注

    Try like this

    select table1.firstname, table2.lastname from table1, table2 where table1.id = table2.id

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序