2014-04-17 51 views
0

工作到我FPDF从MySQL转换为SQL-Server,但得到这个错误:错误:查询:资源ID#4

Error: 
Query: Resource id #4 

在我的代码我从MYSQL改变了这一切,以SQLSRV:

生成-pdf.php

<form action='' method='Post'/> 

<input type='submit' name='submit' value='Generer rapport'> 

</form> 



<?php 

require('mysql_table.php'); 

$timezone = "Europe/Oslo"; 
date_default_timezone_set($timezone); 
$format="%H%M%S"; 
$strf=strftime($format); 

if(isset($_POST['submit'])) 

{ 


class PDF extends PDF_MySQL_Table 
{ 


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

//Connect to database 
$conn_array = array (
"UID" => "sa", 
"PWD" => "root", 
"Database" => "chart", 
) ; 
$conn = sqlsrv_connect('BILAL' , $conn_array); 


$pdf=new PDF(); 
$pdf->AddPage(); 
//First table: put all columns automatically 
$pdf->Table($conn, "SELECT PH, Temperature FROM chartgoogle"); 
$prop=array('HeaderColor'=>array(255,150,100), 
      'color1'=>array(210,245,255), 
      'color2'=>array(255,255,210), 
      'padding'=>2); 
$pdf->Output($downloadfilename."$strf.pdf"); 
header('Location: '.$downloadfilename."$strf.pdf"); 
} 
?> 

mysql_table.php

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

class PDF_MySQL_Table extends FPDF 
{ 
var $ProcessingTable=false; 
var $aCols=array(); 
var $TableX; 
var $HeaderColor; 
var $RowColors; 
var $ColorIndex; 



function Footer() 
{ 
    //Position at 1.5 cm from bottom 
    $this->SetY(-10); 
    //Arial italic 12 
    $this->SetFont('Arial','I',12); 
    //Page number 
    $this->Cell(0,10,'Page '.$this->PageNo().'',0,0,'C'); 
} 



function Header() 
{ 
    //Print the table header if necessary 
    if($this->ProcessingTable) 
     $this->TableHeader(); 
} 

function TableHeader() 
{ 
    $this->SetFont('Arial','B',12); 
    $this->SetX($this->TableX); 
    $fill=!empty($this->HeaderColor); 
    if($fill) 
     $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]); 
    foreach($this->aCols as $col) 
     $this->Cell($col['w'],6,$col['c'],1,0,'C',$fill); 
    $this->Ln(); 
} 

function Row($data) 
{ 
    $this->SetX($this->TableX); 
    $ci=$this->ColorIndex; 
    $fill=!empty($this->RowColors[$ci]); 
    if($fill) 
     $this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]); 
    foreach($this->aCols as $col) 
     $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill); 
    $this->Ln(); 
    $this->ColorIndex=1-$ci; 
} 

function CalcWidths($width,$align) 
{ 
    //Compute the widths of the columns 
    $TableWidth=0; 
    foreach($this->aCols as $i=>$col) 
    { 
     $w=$col['w']; 
     if($w==-1) 
      $w=$width/count($this->aCols); 
     elseif(substr($w,-1)=='%') 
      $w=$w/100*$width; 
     $this->aCols[$i]['w']=$w; 
     $TableWidth+=$w; 
    } 
    //Compute the abscissa of the table 
    if($align=='C') 
     $this->TableX=max(($this->w-$TableWidth)/2,0); 
    elseif($align=='R') 
     $this->TableX=max($this->w-$this->rMargin-$TableWidth,0); 
    else 
     $this->TableX=$this->lMargin; 
} 

function AddCol($field=-1,$width=-1,$caption='',$align='L') 
{ 
    //Add a column to the table 
    if($field==-1) 
     $field=count($this->aCols); 
    $this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align); 
} 

function Table($query,$prop=array()) 
{ 

$conn_array = array (
"UID" => "sa", 
"PWD" => "root", 
"Database" => "chart", 
) ; 

$conn = sqlsrv_connect('BILAL' , $conn_array); 
    //Issue query 
$res=sqlsrv_query($conn, '$query') or die('Error: '.mysql_error()."<BR>Query: $query"); 


    //Add all columns if none was specified 
    if(count($this->aCols)==0) 
    { 
     $nb=sqlsrv_num_fields($res); 
     for($i=0;$i<$nb;$i++) 
      $this->AddCol(); 
    } 
    //Retrieve column names when not specified 
    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(sqlsrv_get_field($res,$col['f'])); 
     } 
    } 
    //Handle properties 
    if(!isset($prop['width'])) 
     $prop['width']=0; 
    if($prop['width']==0) 
     $prop['width']=$this->w-$this->lMargin-$this->rMargin; 
    if(!isset($prop['align'])) 
     $prop['align']='C'; 
    if(!isset($prop['padding'])) 
     $prop['padding']=$this->cMargin; 
    $cMargin=$this->cMargin; 
    $this->cMargin=$prop['padding']; 
    if(!isset($prop['HeaderColor'])) 
     $prop['HeaderColor']=array(); 
    $this->HeaderColor=$prop['HeaderColor']; 
    if(!isset($prop['color1'])) 
     $prop['color1']=array(); 
    if(!isset($prop['color2'])) 
     $prop['color2']=array(); 
    $this->RowColors=array($prop['color1'],$prop['color2']); 
    //Compute column widths 
    $this->CalcWidths($prop['width'],$prop['align']); 
    //Print header 
    $this->TableHeader(); 
    //Print rows 
    $this->SetFont('Arial','',8); 
    $this->ColorIndex=0; 
    $this->ProcessingTable=true; 
    while($row=sqlsrv_fetch_array($res)) 
     $this->Row($row); 
    $this->ProcessingTable=false; 
    $this->cMargin=$cMargin; 
    $this->aCols=array(); 
} 
} 
?> 
+0

不知道它有什么与SQ​​L Server做。 – Rahul

+0

我改变了所有的mysql等于sqlsrv,与MySQL工作正常,但我很不确定为什么它的错误。因为它应该工作。 – user3185936

回答

2

您将错误的参数传递给您的Table()方法。

这是方法需要什么,查询和属性的数组:

Table($query,$prop=array()) 

这是你的过客,一个连接实例和查询。

$pdf->Table($conn, "SELECT PH, Temperature FROM chartgoogle"); 

尝试上述更改为,

$pdf->Table("SELECT PH, Temperature FROM chartgoogle"); 
+0

@ user3185936你在file,generate-pdf.php?在这一行? //第一个表:自动放置所有列 $ pdf->表($ conn,“SELECT PH,Temperature FROM chartgoogle”); –

+0

做了它,现在得到:错误: 查询:SELECT PH,温度FROM chartgoogle – user3185936

+0

我在mysql_table做的唯一更改是将mysql更改为sqlsrv equlent函数,我希望这不是错误。 – user3185936