Monday, April 13, 2009

PHP and SQL SERVER 2005

it seems that the connection was unstable....
im using mssql_pconnect to connect PHP to SQL SERVER 2005.

anyway now im trying a new way to connect PHP and SQL SERVER 2005
and thats using ADODB Connection Object.

to use this you have to call COM class from PHP

function adodb_query($sql){
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=SQLOLEDB; Data Source=".$this->ado_data_source.";
Initial Catalog=".$this->ado_db_name."; User ID=".$this->ado_db_uid."; Password=".$this->ado_db_pwd."");


$rs = $conn->Execute($sql);
$num_columns = $rs->Fields->Count();
$ado_field_name = array();
for ($i=0; $i < $num_columns; $i++) {
$ado_field_name[] = $rs->Fields($i)->Name;
}

$result = array();
$test = array();
$rowcount = 0;
$rs->MoveFirst;
while (!$rs->EOF) {
for ($i=0; $i < $num_columns; $i++) {
$result[$rowcount][$ado_field_name[$i]] = $rs->Fields[$i]->value;

}

$rs->MoveNext();
$rowcount++;

}
$rs->Close();
$conn->Close();
$conn = null;
return $result;
}

this function will execute query and return it as an array.
it was the same with mssql_fetch_array().

but this has a huge problem.
because of everytime you call the function its create a new connection.
its just mean a longer time to wait.

No comments:

Starting to Learn Wordpress

 I'm gonna start learning wordpress again. I know it's been a while and I'm using Blogger to write everything.  I don't know...