php - 一个html表中的双重查询[PHP/mysql]

标签 php mysql sql

伙计们,我有类似的东西可以将结果放入 html 表中。代码如下:

<?php

$db_host = 'localhost'; // Server Name
$db_user = 'username'; // Username
$db_pass = 'password'; // Password
$db_name = 'database'; // Database Name

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = "select * FROM Table WHERE test ='10'";  
$query = mysqli_query($conn, $sql);

if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}


?>
        <table id="MyTable" class="display" cellspacing="0" width="100%">  
        <br><br>
        <thead>
            <tr>
                <th>NO</th>
                <th><?=$this->lang->line('NO');?> [h]</th>
                <th><?=$this->lang->line('RESULTS1');?></th>
                <th><?=$this->lang->line('RESULTS2');?></th>
                <th><?=$this->lang->line('RESULTS3');?></th>
            </tr>
        </thead>
        <tbody>
        <?php
        $no     = 0;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            $amount  = $row['time_spent'] == 0 ? '' : number_format($row['amount']);
            echo '<tr>
                    <td>'.$no.'</td>
                    <td>'.$row['result1'].'</td>
                    <td>'.$row['result2'].'</td>
                    <td>'.$row['result3'].'</td>
                    <td>'.$row['reg_date'].'</td>
                    <td>'.$row['result from another ($sql)'].'</td>
                </tr>';
            $total += $row['result1'];

            $no++;
        }?>
        </tbody>
        </tbody>
            <tr>
                <td colspan="3"><?=$this->lang->line('application_total');?></td>
                <th><? echo $total_time;?></th>
            </tr>
        </tbody>
    </table>

一切都很好,但是是否有可能向此代码添加第二个 sql 查询,以便我将结果放入一些 <td>...</td>在如上所示的同一个表中?

最佳答案

Everything is fine but is there any possible to add to this code a second sql query..

当然有可能。

你可以这样做“select * FROM another_Table WHERE the_id ='”。 $row["relation_id"] ."'";

但是如果关系是 1-1,那么我建议您使用连接表而不是执行新查询。

即:

select a.*, b.another_field 
from table1 a
join table2 b on b.relation_id = a.relation_id

关于php - 一个html表中的双重查询[PHP/mysql],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501002/

相关文章:

php - 如何修复yii2中缺少的命名空间

c# - 将日期插入 MySql 表时阻止所有星期三和星期四

mysql - 三个表之间的 SQL 查询 - 哪个连接?

sql - 如何将运算符连接到 where 子句中?

SQL 更新数据集中的位置

php - 合并数组,即使它们不存在于 PHP

php - 在迷你购物车中显示 Woocommerce 订阅重复总数

php - 如何强制 https 用于 prod 但 http 用于开发环境?

php - 无法在MySQL和php中使用关联数组

php - 登录成功后显示用户名