dpd7122 2017-06-21 16:04
浏览 59
已采纳

(php,mysql)根据同一db中另一个表中的属性显示一个MySQL表的结果[duplicate]

I'm trying to create a search bar in PHP where I would write name of the movie ( full name or just a few first letters of the name) and it would display information about actors in that movie such as actor's firstname, lastname and role. I'm using MySQL and there would be 3 tables name ( column names ):

actors ( a_id, firstname, lastname, role in the movie)
movies ( m_id, name )
actors_movies ( actor_id, movie_id )

i.e. I'd write Lord of the and it would display Elijah Wood, Frodo Baggins etc. I can't get this query to work at all, so any help would be greatly appreciated.

<?php
$connection = @mysql_connect('localhost', 'root', '');
$db = mysql_select_db('db-name', $connection);
$term = strip_tags(substr($_POST['searchit'], 0, 100));

//$term = mysql_escape_string($term); // Attack Prevention
if ($term == "") {
    echo "Enter Something to search";
} else {
    $query = mysql_query("SELECT a.firstname, a.lastname, a.role, m.name 
          FROM actors a 
          INNER JOIN actors_movies am ON a.a_id = am.actor_id 
          INNER JOIN movies m ON am.movie_id = m.m_id 
          ORDER BY a.firstname 
          WHERE m.name like '{$term}%'", $connection);

    $string = '';


    if (mysql_num_rows($query)) {
        while ($row = mysql_fetch_assoc($query)) {
            $string .= $row['firstname'] . "";
            $string .= $row['lastname'] . "</a> , ";
            $string .= $row['role'] . "</a>";
            $string .= "<br/>
";
        }
    } else {
        $string = "No matches found!";
    }

    echo $string;
}
?>

Edit: So after inserting if ( $query === false ) { echo mysql_error(); exit;} and editing the myqsl_query like the first answer below, this is the message that I receive:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:/location.php on line 18
No matches found!You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near actor_id INNER JOIN movies m ON am.movie_id = m.m_id WHERE LOW' at line 3

Name of my columns may include letters that aren't in the English/US vocabulary such as Š,Č etc, could they cause syntax error aswell or that is completely irrelevant?

</div>
  • 写回答

1条回答 默认 最新

  • douyou7072 2017-06-21 16:13
    关注

    You can use % on either side of the search string and use LOWER for case insensitive matching, e.g.:

    SELECT a.firstname, a.lastname, a.role, m.name 
    FROM actors a INNER JOIN actors_movies am ON a.a_id = am.actor_id 
    INNER JOIN movies m ON am.movie_id = m.m_id 
    WHERE LOWER(m.name) like '%{$term}%'
    ORDER BY a.firstname;
    

    Here's how you can do it with prepared statement:

    $stmt = $conn->prepare("SELECT a.firstname, a.lastname, a.role, m.name 
        FROM actors a INNER JOIN actors_movies am ON a.a_id = am.actor_id 
        INNER JOIN movies m ON am.movie_id = m.m_id 
        WHERE LOWER(m.name) like '%:term%'
        ORDER BY a.firstname");
    $stmt->bindParam(':term', $term);
    $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c