douji1999 2014-04-09 05:14
浏览 31

2表MYSQL PHP查询和应用程序用PHP填充HTML网格显示

Hello and thank you for viewing my question, I'll get into it.

I'm looking to pull a bunch of data stored in two different tables within a MySQL database, assign values from each row into arrays (if this is the best approach), then use this data to populate html grid elements (so that each row is used to populate one box in the grid).

This will end up as a grid preview for threads within a php-mysql webapp, a user will have threads displayed as 'preview items', which they may click to open the thread in full. So far this is how I'm approaching the task, do you have any tips or recommendations on how you would do the same?

idea_thread contains information about the threads, and thumb contains the images used for thumbnails and grid box side.

    $result = $stmt = $mysqli->query("SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour 
FROM idea_thread i, thumb t
    WHERE t.idea_id = i.idea_id 
        ORDER BY i.idea_id DESC LIMIT 150,5");  
while ($row = $stmt->fetch_array(MYSQLI_ASSOC)){

        $id = $row[0];
        $user = $row[1];
        $date = $row[2];
        $title = $row[3];
        $briefdescrip = $row[4];
        $photolocation = $row[5];
        $width = $row[6];
        $height = $row[7];
        $color = $row[8];
};

?>

Thank you for reading and for any tips you may offer.

For somereason this code however returns nothing but a white screen? Any thoughts as to why?

Edits applied as a result of suggestions, reflected in my code **

  • 写回答

2条回答 默认 最新

  • doutan1905 2014-04-09 05:19
    关注

    You are using both mysql (mysql_fetch_array) and mysqli and are you sure you want an outer join? A simple way to join is just specifying multiple tables with an identifier like you have (i and t) and then specifying where they are joined like so:

    SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour FROM idea_thread i, thumb t WHERE t.idea_id = i.idea_id ORDER BY i.idea_id DESC

    A left outer join will include all threads even without a correlated id in thumb which may be what you are after:

    SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour FROM idea_thread i LEFT OUTER JOIN thumb t ON t.idea_id = i.idea_id ORDER BY i.idea_id DESC

    Since you aren't providing any values, you don't need to use prepare and can just use query with mysqli.

    Update: Noticed you had * in the SELECT as well. This will select all the columns then the ones you specified throwing off your query even more.

    So for mysqli, you will want:

    $stmt = $mysqli->query(...);
    while ($row = $stmt->fetch_array(MYSQLI_NUM)) {
    
     ... logic here ...
    
    }
    

    I used MYSQLI_NUM in fetch_array since you are referencing the columns by number, you can also use MYSQLI_ASSOC: http://us2.php.net/manual/en/mysqli-result.fetch-array.php

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错