dt4233 2016-06-20 09:53
浏览 43

以pdf格式输出MYSQL内容

I'm not sure what I am doing wrongly but from the code below I can download a pdf file but without the contents. It only displays the column headers. Someone please point out anything am doing wrong, thank you.

//Connect to your database

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

 // Create con

 $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }


//Select the what you want to show in your PDF file
$result = mysqli_query($conn, "SELECT id, name, amount, trans_id, msisdn,   time_paid, status FROM customer");




//$number_of_products = $result->num_rows;



 //Initialize the columns and the total
$column_id = "";
$column_name = "";
$column_amount = "";
$column_trans_id = "";
$column_msisdn = "";
$column_time_paid = "";
$column_status = "";
$total = 0;

//For each row, add the field to the corresponding column
while($row = mysqli_fetch_array($result)) 
 {
$id = $row["id"];
$name = $row["name"];
$real_amount = $row["amount"];
$trans_id = $row["trans_id"];
$msisdn = $row["msisdn"];
$time_paid = $row["time_paid"];
$status = $row["status"];



$column_id = $column_id.$id."
";
$column_name = $column_name.$name."
";
$column_amount = $column_amount.$real_amount."
";
$column_trans_id = $column_trans_id.$trans_id."
";
$column_msisdn = $column_msisdn.$msisdn."
";
$column_time_paid = $column_time_paid.$time_paid."
";
$column_status = $column_status.$status."
";

//Sum all the Prices (TOTAL)
$total = $total+$real_amount;
}
 //mysql_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(50,6,'Name',1,0,'L',1);
$pdf->SetX(65);
$pdf->Cell(100,6,'Amount',1,0,'L',1);
$pdf->SetX(85);
$pdf->Cell(200,6,'Mobile Number',1,0,'L',1);
$pdf->SetX(105);
$pdf->Cell(200,6,'Transation ID',1,0,'L',1);
$pdf->SetX(105);
$pdf->Cell(200,6,'Time Paid',1,0,'L',1);
$pdf->SetX(165);
$pdf->Cell(300,6,'Status',1,0,'R',1);
$pdf->Ln();

 //Now show the 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(85);
 $pdf->MultiCell(100,6,$column_amount,1);
 $pdf->SetY($Y_Table_Position);
 $pdf->SetX(135);
 $pdf->MultiCell(100,6,$column_msisdn,1);
 $pdf->SetY($Y_Table_Position);
 $pdf->SetX(105);
 $pdf->MultiCell(100,6,$column_time_paid,1);
 $pdf->SetY($Y_Table_Position);
 $pdf->SetX(135);
 $pdf->MultiCell(30,6,$column_status,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;
  }

 $pdf->Output();

?> 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测