php - 我只能下载表头,不能下载FPDF中MYSQL数据库的数据

标签 php mysql pdf mysqli fpdf

这是我将 MYSQL 数据下载为 PDF 的代码。但是,我只能显示表标题,而不能显示表中的数据。尝试了多种方法但均无效。我不确定我做错了什么。任何人?

<?php
ob_start();
require('fpdf/fpdf.php');


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//

Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

//Select the Products you want to show in your PDF file
$result= "SELECT name, trans_id, amount FROM bustomer";
$number_of_products = mysqli_num_rows($result);



//Initialize the 3 columns and the total
$column_name = "";
$column_trans_id = "";
$column_amount = "";
$total = 0;

//For each row, add the field to the corresponding column
while($row = mysqli_fetch_array($result))
{
    $name = $row["name"];
    $trans_id = substr($row["trans_id"],0,20);
    $amount = $row["amount"];
    $price_to_show = number_format($row["amount"],',','.','.');

    $column_name = $column_name.$name."\n";
    $column_trans_id = $column_trans_id.$trans_id."\n";
    $column_amount = $column_amount.$price_to_show."\n";

    //Sum all the Prices (TOTAL)
    $total = $total+$real_price;
}
mysqli_close();

//Convert the Total Price to a number with (.) for thousands, and (,) for decimals.
$total = number_format($total,',','.','.');

//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();

//Fields Name position
$Y_Fields_Name_position = 20;
//Table position, under Fields Name
$Y_Table_Position = 26;

//First create each Field Name
//Gray color filling each Field Name box
$pdf->SetFillColor(232,232,232);
//Bold Font for Field Name
$pdf->SetFont('Arial','B',12);
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(45);
$pdf->Cell(20,6,'NAME',1,0,'L',1);
$pdf->SetX(65);
$pdf->Cell(100,6,'TRANSACTION ID',1,0,'L',1);
$pdf->SetX(135);
$pdf->Cell(30,6,'AMOUNT',1,0,'R',1);
$pdf->Ln();

//Now show the 3 columns
$pdf->SetFont('Arial','',12);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(45);
$pdf->MultiCell(20,6,$column_name,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(65);
$pdf->MultiCell(100,6,$column_trans_id,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(135);
$pdf->MultiCell(30,6,$columna_amount,1,'R');
$pdf->SetX(135);
$pdf->MultiCell(30,6,'$ '.$total,1,'R');

//Create lines (boxes) for each ROW (Product)
//If you don't use the following code, you don't create the lines separating each row
$i = 0;
$pdf->SetY($Y_Table_Position);
while ($i < $number_of_products)
{
    $pdf->SetX(45);
    $pdf->MultiCell(120,6,'',1);
    $i = $i +1;
}
ob_end_clean();
$pdf->Output();
ob_end_flush();
?>

    enter code here

最佳答案

作为社区维基发布,因为我自己已经在评论中解决了这个问题,但我没有从中获得任何好处。

您从未执行过查询。

这里:

$result= mysqli_query($conn, "SELECT name, trans_id, amount FROM bustomer"); 

并检查错误。

关于php - 我只能下载表头,不能下载FPDF中MYSQL数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105384/

相关文章:

php - mysql 在数组中的位置

php - 使用 PHP 显示来自 MySQL 数据库的希腊文本的问题

pdf - Powershell 获取超过 x 天的文件并移动它们

PDFsharp 换行符

php - 正则表达式中的双正斜杠

php - 触发器阻止 INSERT

php 从文本文件中获取数据

php - 在 PHP MYSQL 上获取 JSON

php - 排除 "IN"子句中的值

android - 为什么我在 Android 上的 webview 加载缓慢?