简体   繁体   中英

How do I output data from a mysql database in php correctly

I'm having trouble displaying data from my mysql database in form of a pdf document via a php script. I've gotten it right but the column headers doesn't from my database doesn't show when I run the output. furthermore, my table seems to be truncated halfway. Can someone assist me with the right dimensions to get the full table records to display? Thanks.

Here's my code for outputting data in pdf format on a php page

$db_handle = new DBController();

$result =  $db_handle->runQuery('select p.*, s.sname as state_name, 
    l.lname as lga_name from pat_reg p INNER join lga l on p.lga_id = 
    l.lga_id INNER join states s on p.state_id = s.state_id');


$header = $db_handle->runQuery('select p.*, s.sname as state_name, 
    l.lname as lga_name from pat_reg p INNER join lga l on p.lga_id = l.lga_id 
    INNER join states s on p.state_id = s.state_id');



require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',11);
foreach($header as $heading) {
    foreach($heading as $column_heading)
        $pdf->Cell(80,12,$column_heading,1);
    }
    foreach($result as $row) {
        $pdf->SetFont('Arial','',11);
        $pdf->Ln();
        foreach($row as $column)
            $pdf->Cell(80,12,$column,1);
        }

    $pdf->Output();

Here's my database controller class

class DBController {
    private $host = "localhost";
    private $user = "root";
    private $password = "";
    private $database = "patients_db";

    function runQuery($sql) {
        $conn = new mysqli($this->host,$this->user,$this->password,$this->database);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
        $conn->close();
        if(!empty($resultset))
            return $resultset;
    }
}

Any help will be much-appreciated.

I am not familiar with FPDF so I'm just following the code that you provided for that.

<?php
$db_handle = new DBController();

$result =  $db_handle->runQuery('select p.*, s.sname as state_name, 
    l.lname as lga_name from pat_reg p INNER join lga l on p.lga_id = 
    l.lga_id INNER join states s on p.state_id = s.state_id');


// Header query removed

require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',11);

// Add some logic to ensure there is at least one row
if(count($result) > 0){
    // Loop through the keys of the associative array (use the first row)
    foreach($result[0] as $header => $value) {
        $pdf->Cell(80,12,$header,1);
    }
    // Add the line ending here too I assume?
    $pdf->Ln();
    // Loop through rows
    foreach ($result as $row) {
        $pdf->SetFont('Arial','',11);
        $pdf->Ln();
        foreach($row as $column)
            $pdf->Cell(80,12,$column,1);
        }
    }
    // Output PDF
    $pdf->Output();
} else {
    // Error handling
}

The idea is that the results we get from database objects are associative arrays and each follows a key => value pattern. So assuming we have at least 1 entry in the result, we can look at that first entry, loop through the keys and print them as headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM