从多个mysql表中获取数据并以PHP(数组)显示

时间:2013-07-28 09:54:08

标签: php mysql

我有一个带表格的moviesdb

  1. 电影表;
  2. 数据表
  3. 人员表
  4. 字符表
  5. 模式表
  6. 电影表

    ===============================================
    CODE : TITLE :        DESCRIPTION       YEAR
    =====================================================
    1      IRON MAN 3       xxxxx          2013
    2      STAR WARS        xxxxx          2013
    

    人员表

    ===============================================
    CODE : NAME:                 BIRTH_DATE
    =====================================================
    1      SHANE BLACK         1965:04:04 00:00:00
    2      ROBERT DOWNEY       1965:04:04 00:00:00
    3      Gwyneth Paltrow     1965:04:04 00:00:00
    4      Don Cheadle         1965:04:04 00:00:00
    

    字符表

    ===============================================
    PERSON  : NAME:             MOVIE
    =====================================================
    2      TONY STARK          1
    3      PEPPER POTTS        1
    4      Col. James Rhodes   1
    

    模式表

    ===============================================
    CODE : NAME : 
    =====================================================
    1      DIRECTOR 
    2      WRITER
    3      ACTOR
    4      CHARACTER
    

    数据表

    ===============================================
    CODE : MOVIE :     PERSON   MODE
    =====================================================
    1      1             1       1
    2      1             2       3 
    3      1             3       3 
    4      1             4       3 
    

    最终回答如下:

    ===============================================
    CODE : MOVIE TITLE :  DIRECTOR  :     ACTORS           CHARACTERS
    =====================================================
    1      IRON MAN 3     SHANE BLACK   Robert Downey Jr.   Tony Stark
                                        Gwyneth Paltrow     Pepper Potts
                                        Don Cheadle         Col. James 
    

    MySQL命令是:

    SELECT 
      movies.title,
      movies.year,
      persons.name,
      modes.name,
      persons.birth_date,
      characters.name
    FROM
      data
      INNER JOIN movies ON (data.movie = movies.code)
      INNER JOIN persons ON (data.person = persons.code)
      INNER JOIN modes ON (data.mode = modes.code)
      INNER JOIN characters ON (persons.code = characters.person)
      AND (characters.movie = data.movie)
    WHERE
      MOVIES.code = '1'
    

    SQL命令工作正常;

    我遇到了PHP CODE的问题

    $movie_query = mysql_query("
    SELECT 
      movies.title,
      movies.year,
      persons.name,
      modes.name,
      persons.birth_date,
      characters.name
    FROM
      data
      INNER JOIN movies ON (data.movie = movies.code)
      INNER JOIN persons ON (data.person = persons.code)
      INNER JOIN modes ON (data.mode = modes.code)
      INNER JOIN characters ON (persons.code = characters.person)
      AND (characters.movie = data.movie)
    WHERE
      MOVIES.code = '1';");
    
    WHILE ($rows = mysql_fetch_array($movie_query ))
    { 
        $movie_code = $rows['movies'.'code'];
        $movies_title = $rows['movies'.'title'];
        $movies_year = $rows['movies'.'year'];
        $movies_date_add = $rows['movies'.'date_add'];
        $movies_tagline = $rows['movies'.'tagline'];
    
        echo "$movie_code<br>$movies_title<br>$movies_year<br>$movies_date_add<br>$movies_tagline<br>";
    }
    

    我的PHP代码有什么问题..? (似乎SQL命令与MySQL语法不同)

1 个答案:

答案 0 :(得分:3)

1你需要使用PDO而不是使用mysql,请参阅上面的christian-giupponi评论,了解其中的内容

2你有一个“;”在你的sql语句中应删除

2您的查询结果不包括表名

3你无法访问表格中的数据,除非你选择它,所以$ row ['code']不起作用,除非你在sql中要求movies.code

4当您选择具有相同名称的字段时,您需要重新分配它们,否则您将会出现错误

$movie_query = mysql_query("
SELECT 
    movies.code,
    movies.title,
    movies.year,
    persons.name as pname,
    modes.name as mname,
    persons.birth_date,
    characters.name as cname
FROM
    data
    INNER JOIN movies ON (data.movie = movies.code)
    INNER JOIN persons ON (data.person = persons.code)
    INNER JOIN modes ON (data.mode = modes.code)
    INNER JOIN characters ON (persons.code = characters.person)
    AND (characters.movie = data.movie)
WHERE 
    MOVIES.code = '1'
");

WHILE ($rows = mysql_fetch_array($movie_query ))
{ 
    $movie_code = $rows['code'];
    $movies_title = $rows['title'];
    $movies_year = $rows['year'];
    $movies_date_add = $rows['date_add'];
    $movies_tagline = $rows['tagline'];

    echo "$movie_code<br>$movies_title<br>$movies_year<br>$movies_date_add<br>$movies_tagline<br>";
}

祝你好运,请记住,如果你现在不使用PDO,你将会受到影响