Search code examples
phppdfmysqlifpdfgenerated

FPDF/PHP: Column names not retrieved with Mysqli_fetch_fields()


So I have a problem regarding the FPDF library which is used to generate a PDF document with data from a MYSQL-DB.

In PHP 5, all the code is working and I get the following output:

enter image description here

I've used this code for generating it:

<?php
require('mysql_table.php');

class PDF extends PDF_MySQL_Table
{
function Header()
{

    $this->SetFont('Arial','',18);
    $this->Cell(0,6,'test',0,1,'C');
    $this->Ln(10);
    //Ensure table header is output
    parent::Header();
}
}

//Connect to database
mysql_connect('localhost','root','');
mysql_select_db('testDB');

$pdf=new PDF();
$pdf->AddPage();

$prop=array('HeaderColor'=>array(255,150,100),
            'color1'=>array(210,245,255),
            'color2'=>array(255,255,210),
            'padding'=>2);
$pdf->Table('SELECT `first_name`, `last_name`, `title` FROM a3324_userWHERE DELETED = 0 order by `date_entered`',$prop);

header('Content-type: test/pdf');
$pdf->Output('test'.".pdf", 'D'); 

?>

When I tried to achieve the same in my Linux Server, it noted me that I should use mysqli_* since the old version is not supported by PHP 7.

I have replaced all functions by their mysqli-variant and have come to the following result: enter image description here

As you can see, it works except it doesn't retrieve the column names from the database. I have done some research and noted that the old function to retrieve the column names is the following:

foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst(mysql_field_name($res,$col['f']));
        }
    }

Since I was needed to use mysqli, I found that the function mysql_field_name() is not used anymore and got replaced by mysqli_fetch_field()or mysqli_fetch_fields().

I have tried to replace the mysql_field_name($res,$col['f']) by mysqli_fetch_fields($res,$col['f']) or by mysqli_fetch_field() but this didn't work either.

Where could my problem be situated?


Solution

  • Try:

    foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst(mysqli_fetch_field_direct($res, $col['f'])->name);
        }
    }
    

    or use:

    function mysqli_field_name($res, $fieldnr) {
        $finfo = mysqli_fetch_field_direct($res, $fieldnr);
        return is_object($finfo) ? $finfo->name : false;
    }