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条)

报告相同问题?

悬赏问题

  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库