du_1993 2016-06-03 12:51
浏览 19
已采纳

输出mysql结果的问题

I create a form to register company and insert into DB. also created a form to insert each company cost and sell price. after all this has been inserted in DB, i try to output it on table but it was showing the company id instead of name... please bellow illustrate better

company DB

|id | company_name |
|1  | Oliva Ltd    |
|2  | Bone Mill    |

comp_product DB

|id | company_id   | cp     | sp    |
|3  | 2            | 20000  | 18000 |
|4  | 1            | 3000   | 100   |

The help I need is to output all data from comp_product DB by representing the company_id with their company_name from company DB.

Ouput Table

| company_id   | cp     | sp    |
| Bone Mill    | 20000  | 18000 |
| Oliva Ltd    | 3000   | 100   |

i tried bellow code, but i was showing only one row.

<table>
                          <thead>
                          <tr>
                              <th>company_id</th>
                              <th>cp</th>
                              <th>sp</th>
                          </tr>
                          <?php 

                          $result = mysql_query("SELECT * FROM comp_product ORDER BY id DESC");
                                while($row = mysql_fetch_array($result))
                                {
                                    $result = mysql_query("SELECT * FROM company WHERE id ='".$row['company_id']."'");
                                            while($rowz = mysql_fetch_array($result))
                                                { $name = $rowz['company_name']; }
                                    ?>
                          <tr>
                              <td><?php echo $name ?></td>
                              <td><?php echo $row['cp'] ?></td>
                              <td><?php echo $row['cp'] ?></td>
                          </tr>
                          <?php 
                                }
                                ?>
                      </table>

Show can I do this. Thanks for understanding

  • 写回答

4条回答 默认 最新

  • dstobkpm908182 2016-06-03 12:57
    关注

    You need a JOIN query

    SELECT `company`.`company_name`, `comp_product`.`cp`, `comp_product`.`sp`
    FROM `company`
    LEFT JOIN `comp_cp`
    ON `company`.`id` = `comp_product`.`company_id`
    

    You do not need two queries, you can just loop through the results of one. If you need to limit this to one company you can add a WHERE clause.


    In addition you need to stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分