dongqing4774 2013-07-28 09:54
浏览 23
已采纳

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

i have a moviesdb with tables

  1. movies table ;
  2. data table
  3. persons table
  4. characters table
  5. Modes table

MOVIES TABLE

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

PERSONS TABLE

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

CHARACTERS TABLE

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

MODES TABLE

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

DATA TABLE

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

FINAL ANSWER WOULD BE like :

===============================================
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 command is :

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;

i am having problem with 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>";
}

any problem with my PHP CODE ..? ( seems SQL commands are not same as MySQL syntax )

  • 写回答

1条回答 默认 最新

  • dtq26360 2013-07-28 10:38
    关注

    1 you need to use PDO instead of using mysql see christian-giupponi comment above for insturctions on that

    2 you have a ";" in your sql statment that should be removed

    2 the results of your query do not include the table name

    3 you can't access data in the table unless you select it so $row['code'] would not work unless you ask for movies.code in your sql

    4 when you are selecting fields with the same name you need to reassign them or you will end up with something wrong

    $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>";
    }
    

    Good luck and Remember if you don't USE PDO now you will suffer for it later

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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