2016-07-13 29 views
1

所以我有一個關於FPDF庫的問題,它用於生成帶有來自MYSQL-DB的數據的PDF文檔。FPDF/PHP:未使用Mysqli_fetch_fields()檢索列名

在PHP 5中,所有的代碼工作,我得到下面的輸出:

enter image description here

我用這個代碼生成它:

<?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'); 

?> 

當我試圖在我的Linux服務器上實現同樣的功能,它指出我應該使用mysqli_ *,因爲PHP 7不支持舊版本。

我因他們的mysqli變替換的所有功能,並得出以下結果: enter image description here

正如你所看到的,它的工作原理只是它不從數據庫中檢索列名。我做了一些研究,並指出,舊的函數來檢索列名如下:

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'])); 
     } 
    } 

因爲我需要用mysqli的,我發現功能mysql_field_name()不再使用,並得到了由mysqli_fetch_field()取代或mysqli_fetch_fields()

我試圖用mysqli_fetch_fields($res,$col['f'])mysqli_fetch_field()替換mysql_field_name($res,$col['f']),但是這也不起作用。

我的問題在哪裏?

回答

1

嘗試:

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); 
    } 
} 

或使用:

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

謝謝!我沒有發現'mysqli_fetch_field_direct()'函數!我的代碼現在工作非常好! –