Bob Mwenda Bob Mwenda - 5 months ago 18
SQL Question

I can only download the table headers but not the data from MYSQL database in FPDF

Here is my code to download MYSQL data into PDF. However, I can only display table headers but not the data in the tables. Tried a number of ways but not working. I'm not sure what I am doing wrong. Any one?

<?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

Answer

Posting as a community wiki, as it has been solved in comments by myself and I've nothing to gain from this.

You never executed the query.

Here:

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

and check for errors.