如何用一个查询显示多个表
How to show several tables with a single query
我正在尝试显示几个表,其中的特定元素只能使用单个数据库查询列出。我正在使用 IF 来调节特定元素的显示以分隔信息。但是我不能,我不知道语法是否不正确或者是否可以这样做。
<?php
require_once('const.php');
$conn = mysqli_connect(DBHOSTNAME,DBUSERNAME,DBPASSWORD,DBDATABASE) or exit('Error:'.mysqli_connect_error());
mysqli_set_charset($conn,'utf8');
$query = 'SELECT product.nome AS nome, category.name AS category, product.value
FROM product INNER JOIN category ON product.category_id=category.id';
$res = mysqli_query($conn,$query) or exit('Error:'.mysqli_error($conn));
?>
<table>
<tr>
<th colspan="4"><p>Plates</p></th>
</tr>
<?php
if(isset mysqli_query($query['category']) == ("Plates")){
while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<td></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['value']; ?></td>
<td></td>
</tr>
<?php
}
}
mysqli_free_result($res);
mysqli_close($conn);
?>
</table>
哦,我想我现在明白了。您想查询数据库一次,但在页面的多个位置使用部分结果集。
因此 运行 查询并将结果集放入一个数组中,您可以根据需要一次又一次地使用该数组。
<?php
require_once('const.php');
$conn = mysqli_connect(DBHOSTNAME,DBUSERNAME,DBPASSWORD,DBDATABASE);
mysqli_set_charset($conn,'utf8');
$query = 'SELECT product.nome AS nome, category.name AS category, product.value
FROM product
INNER JOIN category ON product.category_id=category.id';
$res = mysqli_query($conn,$query);
$all_results = []; // init the array
while ( $row = $res->fetch_assoc() ) {
$all_results[] = $row;
}
#some drives allow fetch_all() so you could replace above while loop with
#$all_results = $res->fetch_all(); // get all the results one call
?>
<table>
<tr>
<th colspan="4"><p>Plates</p></th>
</tr>
<?php
// using the array, pick only the Plates category rows for this sction of the page
foreach ( $all_results as $row ) {
if ( $row['category']) == "Plates" ){
?>
<tr>
<td></td>
<td><?php echo $row['nome']; ?></td>
<td><?php echo $row['value']; ?></td>
<td></td>
</tr>
<?php
}
}
?>
</table>
我正在尝试显示几个表,其中的特定元素只能使用单个数据库查询列出。我正在使用 IF 来调节特定元素的显示以分隔信息。但是我不能,我不知道语法是否不正确或者是否可以这样做。
<?php
require_once('const.php');
$conn = mysqli_connect(DBHOSTNAME,DBUSERNAME,DBPASSWORD,DBDATABASE) or exit('Error:'.mysqli_connect_error());
mysqli_set_charset($conn,'utf8');
$query = 'SELECT product.nome AS nome, category.name AS category, product.value
FROM product INNER JOIN category ON product.category_id=category.id';
$res = mysqli_query($conn,$query) or exit('Error:'.mysqli_error($conn));
?>
<table>
<tr>
<th colspan="4"><p>Plates</p></th>
</tr>
<?php
if(isset mysqli_query($query['category']) == ("Plates")){
while($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<td></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['value']; ?></td>
<td></td>
</tr>
<?php
}
}
mysqli_free_result($res);
mysqli_close($conn);
?>
</table>
哦,我想我现在明白了。您想查询数据库一次,但在页面的多个位置使用部分结果集。
因此 运行 查询并将结果集放入一个数组中,您可以根据需要一次又一次地使用该数组。
<?php
require_once('const.php');
$conn = mysqli_connect(DBHOSTNAME,DBUSERNAME,DBPASSWORD,DBDATABASE);
mysqli_set_charset($conn,'utf8');
$query = 'SELECT product.nome AS nome, category.name AS category, product.value
FROM product
INNER JOIN category ON product.category_id=category.id';
$res = mysqli_query($conn,$query);
$all_results = []; // init the array
while ( $row = $res->fetch_assoc() ) {
$all_results[] = $row;
}
#some drives allow fetch_all() so you could replace above while loop with
#$all_results = $res->fetch_all(); // get all the results one call
?>
<table>
<tr>
<th colspan="4"><p>Plates</p></th>
</tr>
<?php
// using the array, pick only the Plates category rows for this sction of the page
foreach ( $all_results as $row ) {
if ( $row['category']) == "Plates" ){
?>
<tr>
<td></td>
<td><?php echo $row['nome']; ?></td>
<td><?php echo $row['value']; ?></td>
<td></td>
</tr>
<?php
}
}
?>
</table>