对两个或多个表运行查询后,是否可以打印一组数据?
这来自我查询的两个表:
第一查询
$query = mysql_query("SELECT id, BuyerName,BuyerEmail,BuyerAddress,TransactionID,
ItemAmount,DateTime FROM `order` WHERE YEAR(DateTime)=
'$Year'AND MONTH(DateTime) = '$Month' LIMIT $start, $per_page")
or die(mysql_error());
第二查询
$querydetail = mysql_query("SELECT * FROM orderlist WHERE TransactionID = '$f4'");
是否可以通过使用表将其以php中设置的方式打印出来?
这就是我以表格格式显示数据的方式。有没有办法将此输出直接导入到Microsoft Word或A4纸格式的内容中?
<?php
while($row = mysql_fetch_array($query))
{
$f1 = $row['BuyerName'];
$f2 = $row['BuyerEmail'];
$f3 = $row['BuyerAddress'];
$f4 = $row['TransactionID'];
$f5 = $row['ItemAmount'];
$f6 = $row['DateTime'];
?>
<table class="table">
<tr>
<th>Nama Pelanggan</th>
<th>Email Pelanggan</th>
<th>Alamat Pelanggan</th>
<th>ID Transaksi</th>
<th>Harga</th>
<th>Tarikh</th>
<tr>
<td><?php echo $f1 ?></td>
<td><?php echo $f2 ?></td>
<td><?php echo $f3 ?></td>
<td><?php echo $f4 ?></td>
<td><?php echo $f5 ?></td>
<td><?php echo $f6 ?></td>
</tr>
<table class="table">
<tr>
<th>Nama Barang</th>
<th>Kod Barang</th>
<th>Kuantiti</th>
</tr>
<?php
$querydetail = mysql_query("SELECT * FROM orderlist WHERE TransactionID = '$f4'");
while($rows = mysql_fetch_array($querydetail))
{
$fd1 = $rows['ItemName'];
$fd2 = $rows['ItemNumber'];
$fd3 = $rows['ItemQTY'];
?>
<tr>
<td><?php echo $fd1 ?></td>
<td><?php echo $fd2 ?></td>
<td><?php echo $fd3 ?></td>
</tr>
最佳答案
如果您不保存第一个查询的结果,例如在一个变量中,它将在下一个变量执行后立即丢失。执行查询后立即获取查询结果,将其存储在数组中,并在下一步需要所有可用数据后立即使用它们,例如将它们打印出来。那是你问的吗?
体重不再使用mysql_query
,它将在以后的php版本中删除,并且可能使您的代码容易受到sql注入攻击。使用PDO或mysli进行数据库工作要安全得多。
编辑
要从PHP创建Word文档,请查看this question。
或者,您可以只保留HTML标签,用合适的定界字符分隔列(例如,如果您的数据中不使用分号,则使用分号)
<?php $delimiter = ";"; ?>
...
<?php echo $f1 . $delimiter ?>
代替
<td><?php echo $f1 ?></td>
并导入Excel或OpenOffice Calc。这是将CSV MIME类型输出为download而不是浏览器输出的解决方案。
或者您可以例如使用FPDF创建PDF文件。