Monday, April 13, 2009

PHP and SQL SERVER 2005 with ADODB.CONNECTION

hmm it seems someone is using the php COM class to connect to SQL SERVER 2005.
and i think im gonna try this function to.

i found this article from a mailing list here

and im gonna copy paste it to my blog.

thanks for the hardwork AJ.

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

/** 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

....again thanks AJ

testing beta test


function index()
    
{
    
            
/* grab the division list */
            
if ($divisions = $this->main->get_all_divisions())
            
$data['divisions'][$value['id']=0] = '';
            
{
                
foreach ($divisions as $key=>$value)
                
$data['divisions'][$value['id']] = $value['name'];
            
}
    
        $data[
'site_title'] = $this->config->item('site_title');
        
$data['title'] = "Main Page";
        
$this->template->write_view('content', 'default/pages/main_index', $data);
        
$this->template->render();
    
}
    
    
function names()
    
{
       
/* set the POST variable */
        
$division = $this->input->post('division', TRUE);
        
        
/* grab the division details */
        
$item = $this->main->get_division($division);
        
        
/* set the output */
        
$output = ($item !== FALSE) ? $item : '';
        
        echo
$output;
    
}

No comments: