2013-07-28 66 views
2

我有表得到從多個MySQL表和顯示數據在PHP(arrray)

  1. 電影表moviesdb;
  2. 數據表
  3. 者表
  4. 字符表
  5. 模式表

MOVIES TABLE

=============================================== 
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 

FINAL答案將是這樣的:

=============================================== 
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 Command WORKS fine;

我有PHP代碼

$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語法)

+0

in $ movie_query remove;並使用除mysql以外的其他內容。 – Mihai

+0

啓用'error_reporting(E_ALL);'並使用'print_r($ rows)'來查看字段名稱與循環中使用的不同(您將PHP中的表名和列連接起來)。 – mario

+0

@Mihai我明白刪除「;」但「使用別的不是MySQL」?你是否建議他使用不同的數據庫? 和所有爲什麼倒票? – mcgrailm

回答

3

1你需要使用的,而不是用mysql見上基督教giupponi意見,對

2 insturctions PDO你有「;」在應該刪除

2查詢的結果不包括表名的SQL statment

3,除非你選擇它,$行,你不能訪問表中的數據[「代碼」]除非你要求movies.code在SQL

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,您將因此受到影響ter

+1

作品像一個魅力 – user2573165