RE: SQL Server 2005

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok, here is enough code to get you started :-)

<?php
/** this function is here to ensure all locks etc on a connection are
  * cleared at the end of the script.  NEVER call this from a script!
  */
function _adodb_shutdown($connection=null)
{
    // Place any shutdown code here
}

/** This class houses the static methods to creating connections to SQLServer.
  */
class ADODB
{
       
    function ConnectToSQLServer
    (
        $server,
        $database,
        $username,
        $password
    )
    {
        $obj=new COM('ADODB.connection');
        if(!$obj->pinned())
        {
            $obj->open("Provider=SQLOLEDB.1;Server=$server;Database=$database;UID=$username;PWD=$password;");
        }
        register_shutdown_function('_adodb_shutdown');
        return $obj;
    }

    function ExecuteQuery($sql,&$connection)
    {
        $recordsAffected=0;
        return $connection->Execute($sql, $recordsAffected, 8);
    }
   
    function Execute($sql,&$connection)
    {
        $ra=0;
        $connection->Execute($sql,$ra,129);
    }

}

/* Some util functions */

function dbEscape($s)
{
    return "'".str_replace("'", "''", $s)."'";
}

function forceNumber($number)
{
    if(!is_numeric($number))
    {
        // Trigger error is one of my fuctions, you'd have to do somethine else ;-)
        //trigger_error("Non numeric passed to forceNumber: '$number'", E_USER_ERROR);
    }
    return $number*1.0; // ensures it is stored in php as a number
}

/* Some examples of using it.
* Please note that in these examples I connect to the DB in each function,
* this is totally and utterly wrong for a real situation, you should only
* make a connection once per php client connection.
*/

/* Gets a single result */
function exmpl1()
{
    $server=ADODB::ConnectToSQLServer
    (
        $server,
        $database,
        $username,
        $password
    );
    $recset=0;
    $recset=ADODB::ExecuteQuery("SELECT MIN(RowID) FROM T_RowID",$server);
    $ret=$recset->Fields;
    $ret=$ret->item(0);
    $ret=$ret->Value;
    $recset->close();
    return $ret;
}

/* Gets an array of results */
function exmpl2($Name,$FromId)
{
    $server=ADODB::ConnectToSQLServer
    (
        $server,
        $database,
        $username,
        $password
    );
    $recset=0;
    $Name=dbEscape($name);
    $FromId=forceNumber($parent);
    $recset=ADODB::ExecuteQuery("SELECT ToId FROM T_References WHERE KeyName=$Name AND FromId=$FromId",$server);
    $ret=array();
    while(!$recset->EOF())
    {
        $p=$recset->Fields;
        $p=$p->item(0);
        array_push($ret,$p->Value);
        $recset->movenext();
    }
    $recset->close();
    return $ret;
}

/* Runs an update only */
function exmpl3($parent,$name,$child)
{
    $server=ADODB::ConnectToSQLServer
    (
        $server,
        $database,
        $username,
        $password
    );
    $parent=forceNumber($parent);
    $name=substr($name,0,112);
    $name=dbEscape($name);
    $child=forceNumber($child);
    $sql="INSERT INTO T_References (FromId,KeyName,ToId) VALUES ($parent,$name,$server)";
    ADODB::Execute($sql,$this->connection);
}
?>

Cheers

AJ

On Mon, 2006-12-04 at 12:30 +1300, Bruce Cowin wrote:
I'd be interested.  I've used ADO when connecting through VB and C# but in PHP (which I'm relatively new at) I've always used the mssql_* functions.


Regards,

Bruce

>>> git <git@xxxxxxxxxxxxxxx> 2/12/2006 1:43 a.m. >>>
On Wed, 15 Nov 2006 02:17:07 -0800, Frank M. Kromann wrote:

> Hello,
> 
> This is not correct.
> 
> PDO and native mssql_*() functions exists in two fifferent versions. The
> version compiled with Microsofts (old) version of dblib has the
> restrictions mentioned here but the version compiled with FreeTDS
> (php_dblib.dll and php_pdo_dblib.dll) does not.
> 
> - Frank
> 
>> Hello,
>> 
>> I recommend you use PDO and odbc to connect to MS SQL.  Other
> connection
>> methods have limitations with MS SQL including:
>> 
>> 1.) varchars limited to 256
>> 2.) no support for extended characters
>> 
>> Aspen
>> 
>> -----Original Message-----
>> From: Dale Attree [mailto:dale@xxxxxxxxxxxxxxxxxxxxx] 
>> Sent: Tuesday, November 14, 2006 5:14 AM
>> To: php-windows@xxxxxxxxxxxxx 
>> Subject:  SQL Server 2005
>> Importance: High
>> 
>> Hi all,
>> 
>>  
>> 
>> I am having trouble connecting to SQL Server 2005. I have installed it
> with
>> mixed mode authentication.
>> 
>>  
>> 
>> When I call mssql_connect('localhost',$username,$password), I get a PHP
>> error saying unable to connect to server.
>> 
>>  
>> 
>> In the PHP manual, it says something about an "interfaces" file. Where
> would
>> I find this file?
>> 
>>  
>> 
>> Kind Regards,
>> 
>> Dale
>> 
>> 
>>

In general - unless you are connecting from something other than windows
is it most logical to use ADODB and COM to connection with SQL Server.

That is how I have done it for years and it ticks all the boxes, fast
reliable, flexible, supported, EASY.

If anyone is interested enough I am happy to post a description of how to
do it and the issues you might face on one of my blogs

Cheers

AJ

-- 
Cubical Land
www.cubicalland.com 

-- 
git <git@xxxxxxxxxxxxxxx>
www.cubicalland.com

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux