【问题标题】:Converting FPDF from MYSQL to SQL SERVER将 FPDF 从 MYSQL 转换为 SQL SERVER
【发布时间】:2014-04-17 12:20:11
【问题描述】:

我正在努力将我的 FPDF 生成器从 mysql 转换为 SQL 服务器。并且有一些问题。在 mysql_table.php 文件中,我非常不确定用什么替换 mysql 以使其与 SQL SERVER 兼容。我已经用 HERE 标记了它的 mysql 代码。

这是我的 generatepdf.php 文件:

<?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 文件(这里有 mysql 代码)

    <?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())
    {
        //Issue query
 HERE   $res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
        //Add all columns if none was specified
        if(count($this->aCols)==0)
        {
 HERE       $nb=mysql_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
  HERE      $this->aCols[$i] ['c']=ucfirst(mysql_field_name($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;
     HERE   while($row=mysql_fetch_array($res))
                $this->Row($row);
            $this->ProcessingTable=false;
            $this->cMargin=$cMargin;
            $this->aCols=array();
        }
        }
        ?>

【问题讨论】:

  • 您打算使用sqlsrv 驱动程序吗?如果是这样,PHP 手册有一些解释。对于简单的查询,sql和mysql的语法几乎是一样的,除了mysqlLIMIT在SQL中是TOP

标签: php sql sql-server fpdf sqlsrv


【解决方案1】:

最好的方法是利用这个机会使用 PDO,但是第一次设置它可能会很棘手。这并不难,但您需要完成几个步骤。

$dsn = 'mssql:host=localhost;dbname=yourdbname';
$user = 'dbuser';
$password = 'dbpass';

try {
     $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
     echo 'Connection failed: ' . $e->getMessage();
}

$Query = $dsn->prepare("select * from YourTable where `ID` = :id");
$Query->bindParam(':id', 1, PDO::PARAM_INT);
$Query->execute();

$rs = $q->fetchAll(PDO::FETCH_ASSOC);

在大多数情况下,您需要下载 SQL 服务器驱动程序 你可以在这里找到驱动程序 -> http://www.microsoft.com/en-us/download/details.aspx?id=20098

您还需要安装 Microsoft SQL Server 2012 Native Client

您还需要确保启用了 PDO 和 pdo_sqlsrv 扩展。

这里是一些关于 PDO 的文档 -> http://www.php.net/manual/en/class.pdo.php

这是一个示例应用程序 -> http://technet.microsoft.com/en-us/library/ff754357(v=sql.105).aspx

【讨论】:

  • 他们是不同的;每个都有自己的好处。我更喜欢 PDO,因为我喜欢带有例外的 OO。此外,PDO 将更加 DB 中立,如果您需要再次切换,这将使您的生活更轻松。我相信性能应该是相似的,但是 SQLSRV 允许将数据流式传输到数据库。我个人会使用普通的 PDO,但您需要查看需求以了解哪种最适合您的项目。在此处查看一些差异 -> blogs.msdn.com/b/brian_swan/archive/2010/04/20/…
  • 顺便说一句,您仍然需要安装驱动程序和本机客户端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 2019-09-02
  • 2015-06-29
  • 2013-11-29
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
相关资源
最近更新 更多