Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 17th, 2007, 06:18 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default MySQL to MSSQL PHP connection

I'm not sure forum to put this is in, but I was wondering if someone could help with my problem.

I have a php design that gets data from MySQL connection but I need to change it to an MSSQL connection.

Does anyone know how?



Reply With Quote
  #2 (permalink)  
Old December 17th, 2007, 06:30 AM
Friend of Wrox
 
Join Date: Oct 2007
Location: , , .
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

You can do it, you have to user enable mssql extension in php.ini file and restart your webserver. then just go through php manual you will find database function with prefix mssql_????() instead of mysql_???


urt
Reply With Quote
  #3 (permalink)  
Old December 18th, 2007, 01:04 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. I've been looking through the manual as you mentioned, and to be honest im struggling with getting it to work. I'm finding the manuals to be very technical. This is the following code that i have, and not sure what needs to be changed.


<?php
define(FPDF_FONTPATH,'font/');
require('fpdf.php');

class PDF extends FPDF {

var $tablewidths;
var $headerset;
var $footerset;

function _beginpage($orientation) {
    $this->page++;
    if(!$this->pages[$this->page]) // solved the problem of overwriting a page, if it already exists
        $this->pages[$this->page]='';
    $this->state=2;
    $this->x=$this->lMargin;
    $this->y=$this->tMargin;
    $this->lasth=0;
    $this->FontFamily='';
    //Page orientation
    if(!$orientation)
        $orientation=$this->DefOrientation;
    else
    {
        $orientation=strtoupper($orientation{0});
        if($orientation!=$this->DefOrientation)
            $this->OrientationChanges[$this->page]=true;
    }
    if($orientation!=$this->CurOrientation)
    {
        //Change orientation
        if($orientation=='P')
        {
            $this->wPt=$this->fwPt;
            $this->hPt=$this->fhPt;
            $this->w=$this->fw;
            $this->h=$this->fh;
        }
        else
        {
            $this->wPt=$this->fhPt;
            $this->hPt=$this->fwPt;
            $this->w=$this->fh;
            $this->h=$this->fw;
        }
        $this->PageBreakTrigger=$this->h-$this->bMargin;
        $this->CurOrientation=$orientation;
    }
}

function Header()
{
    global $maxY;

    // Check if header for this page already exists
    if(!$this->headerset[$this->page]) {

        foreach($this->tablewidths as $width) {
            $fullwidth += $width;
        }
        $this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
        $this->cellFontSize = $this->FontSizePt ;
        $this->SetFont('Arial','',( ( $this->titleFontSize) ? $this->titleFontSize : $this->FontSizePt ));
        $this->Cell(0,$this->FontSizePt,$this->titleText,0,1,'C');
        $l = ($this->lMargin);
        $this->SetFont('Arial','',$this->cellFontSize);
        foreach($this->colTitles as $col => $txt) {
            $this->SetXY($l,($this->tMargin));
            $this->MultiCell($this->tablewidths[$col], $this->FontSizePt,$txt);
            $l += $this->tablewidths[$col] ;
            $maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
        }
        $this->SetXY($this->lMargin,$this->tMargin);
        $this->setFillColor(200,200,200);
        $l = ($this->lMargin);
        foreach($this->colTitles as $col => $txt) {
            $this->SetXY($l,$this->tMargin);
            $this->cell($this->tablewidths[$col],$maxY-($this->tMargin),'',1,0,'L',1);
            $this->SetXY($l,$this->tMargin);
            $this->MultiCell($this->tablewidths[$col],$this->FontSizePt,$txt,0,'C');
            $l += $this->tablewidths[$col];
        }
        $this->setFillColor(255,255,255);
        // set headerset
        $this->headerset[$this->page] = 1;
    }

    $this->SetY($maxY);
}

function Footer() {
    // Check if footer for this page already exists
    if(!$this->footerset[$this->page]) {
        $this->SetY(-15);
        //Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
        // set footerset
        $this->footerset[$this->page] = 1;
    }
}

function morepagestable($lineheight=8) {
    // some things to set and 'remember'
    $l = $this->lMargin;
    $startheight = $h = $this->GetY();
    $startpage = $currpage = $this->page;

    // calculate the whole width
    foreach($this->tablewidths as $width) {
        $fullwidth += $width;
    }

    // Now let's start to write the table
    $row = 0;
    while($data=mysql_fetch_row($this->results)) {
        $this->page = $currpage;
        // write the horizontal borders
        $this->Line($l,$h,$fullwidth+$l,$h);
        // write the content and remember the height of the highest col
        foreach($data as $col => $txt) {

            $this->page = $currpage;
            $this->SetXY($l,$h);
            $this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]);

            $l += $this->tablewidths[$col];

            if($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
                $tmpheight[$row.'-'.$this->page] = $this->GetY();
            }
            if($this->page > $maxpage)
                $maxpage = $this->page;
            unset($data[$col]);
        }
        // get the height we were in the last used page
        $h = $tmpheight[$row.'-'.$maxpage];
        // set the "pointer" to the left margin
        $l = $this->lMargin;
        // set the $currpage to the last page
        $currpage = $maxpage;
        unset($data[$row]);
        $row++ ;
    }
    // draw the borders
    // we start adding a horizontal line on the last page
    $this->page = $maxpage;
    $this->Line($l,$h,$fullwidth+$l,$h);
    // now we start at the top of the document and walk down
    for($i = $startpage; $i <= $maxpage; $i++) {
        $this->page = $i;
        $l = $this->lMargin;
        $t = ($i == $startpage) ? $startheight : $this->tMargin;
        $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
        $this->Line($l,$t,$l,$lh);
        foreach($this->tablewidths as $width) {
            $l += $width;
            $this->Line($l,$t,$l,$lh);
        }
    }
    // set it to the last page, if not it'll cause some problems
    $this->page = $maxpage;
}

function connect($host='localhost',$username='cstestuser',$ password='secret',$db='cstest'){
    $this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
    mysql_select_db($db,$this->conn) or die( mysql_error() );
    return true;
}

function query($query){
    $this->results = mysql_query($query,$this->conn);
    $this->numFields = mysql_num_fields($this->results);
}

function mysql_report($query,$dump=false,$attr=array()){

    foreach($attr as $key=>$val){
        $this->$key = $val ;
    }

    $this->query($query);

    // if column widths not set
    if(!isset($this->tablewidths)){

        // starting col width
        $this->sColWidth = (($this->w-$this->lMargin-$this->rMargin))/$this->numFields;

        // loop through results header and set initial col widths/ titles/ alignment
        // if a col title is less than the starting col width / reduce that column size
        for($i=0;$i<$this->numFields;$i++){
            $stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 6 ;
            if( ($stringWidth) < $this->sColWidth){
                $colFits[$i] = $stringWidth ;
                // set any column titles less than the start width to the column title width
            }
            $this->colTitles[$i] = mysql_field_name($this->results,$i) ;
            switch (mysql_field_type($this->results,$i)){
                case 'int':
                    $this->colAlign[$i] = 'R';
                    break;
                default:
                    $this->colAlign[$i] = 'L';
            }
        }

        // loop through the data, any column whose contents is bigger that the col size is
        // resized
        while($row=mysql_fetch_row($this->results)){
            foreach($colFits as $key=>$val){
                $stringWidth = $this->getstringwidth($row[$key]) + 6 ;
                if( ($stringWidth) > $this->sColWidth ){
                    // any col where row is bigger than the start width is now discarded
                    unset($colFits[$key]);
                }else{
                    // if text is not bigger than the current column width setting enlarge the column
                    if( ($stringWidth) > $val ){
                        $colFits[$key] = ($stringWidth) ;
                    }
                }
            }
        }

        foreach($colFits as $key=>$val){
            // set fitted columns to smallest size
            $this->tablewidths[$key] = $val;
            // to work out how much (if any) space has been freed up
            $totAlreadyFitted += $val;
        }

        $surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
        for($i=0;$i<$this->numFields;$i++){
            if(!in_array($i,array_keys($colFits))){
                $this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
            }
        }

        ksort($this->tablewidths);

        if($dump){
            Header('Content-type: text/plain');
            for($i=0;$i<$this->numFields;$i++){
                if(strlen(mysql_field_name($this->results,$i))>$flength){
                    $flength = strlen(mysql_field_name($this->results,$i));
                }
            }
            switch($this->k){
                case 72/25.4:
                    $unit = 'millimeters';
                    break;
                case 72/2.54:
                    $unit = 'centimeters';
                    break;
                case 72:
                    $unit = 'inches';
                    break;
                default:
                    $unit = 'points';
            }
            print "All measurements in $unit\n\n";
            for($i=0;$i<$this->numFields;$i++){
                printf("%-{$flength}s : %-10s : %10f\n",
                    mysql_field_name($this->results,$i),
                    mysql_field_type($this->results,$i),
                    $this->tablewidths[$i] );
            }
            print "\n\n";
            print "\$pdf->tablewidths=\n\tarray(\n\t\t";
            for($i=0;$i<$this->numFields;$i++){
                ($i<($this->numFields-1)) ?
                print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */\n\t\t":
                print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t";
            }
            print "\n\t);\n";
            exit;
        }

    } else { // end of if tablewidths not defined

        for($i=0;$i<$this->numFields;$i++){
            $this->colTitles[$i] = mysql_field_name($this->results,$i) ;
            switch (mysql_field_type($this->results,$i)){
                case 'int':
                    $this->colAlign[$i] = 'R';
                    break;
                default:
                    $this->colAlign[$i] = 'L';
            }
        }
    }

    mysql_data_seek($this->results,0);
    $this->Open();
    $this->setY($this->tMargin);
    $this->AddPage();
    $this->morepagestable($this->FontSizePt);
    $this->Output();
}

}

$pdf = new PDF('L','pt','A3');
$pdf->SetFont('Arial','',11.5);
$pdf->AliasNbPages();
$pdf->connect('localhost','cstestuser','secret','cstest ');
$attr=array('titleFontSize'=>18,'titleText'=>'this would be the title');
$pdf->mysql_report("SELECT * FROM members",false,$attr);
?>


Reply With Quote
  #4 (permalink)  
Old December 26th, 2007, 12:36 AM
Friend of Wrox
 
Join Date: Oct 2007
Location: , , .
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

You first seacrh mysql_???? in your code, I found it in mypagetable function and connect function, your code contains mysql word times.
For every mysql_function you will find mssql_??? function in manual,(find file in php manual folder html/ref.mssql.html for mssql and html/ref.mysql.html for mysql)
Replace mysql with mssql after confirming function arguments.
Also please post code for fpdf.php file, i would like to see that code whether it contains any mysql function call or not.

urt
Reply With Quote
  #5 (permalink)  
Old December 26th, 2007, 12:40 AM
Friend of Wrox
 
Join Date: Oct 2007
Location: , , .
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

In second line i meant, I found mysql word 22 times in you code.

urt
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connect to MSSQL without PHP Library echovue Pro PHP 0 April 2nd, 2007 07:19 PM
mysql & php connection problem. Manu Sharma PHP Databases 2 March 2nd, 2007 07:03 AM
PHP and MSSQL on Linux rvanandel PHP Databases 1 November 15th, 2005 08:55 PM
jdbc connection to MSsql & CachedRowSet problems chandrakanth2000 Java Databases 1 September 7th, 2004 04:14 AM
MySQL and MsSQL stevenckh MySQL 2 September 12th, 2003 12:17 AM



All times are GMT -4. The time now is 11:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.