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

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案