gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:21:55 25 4
gpt4 key购买 nike

伙计们,我有类似的东西可以将结果放入 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com