Bob Mwenda Bob Mwenda - 1 year ago 118
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?


$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;

//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();

//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
//Bold Font for Field Name
$pdf->Cell(100,6,'TRANSACTION ID',1,0,'L',1);

//Now show the 3 columns
$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;
while ($i < $number_of_products)
$i = $i +1;

enter code here

Answer Source

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.


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

and check for errors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download