PHP‎ > ‎

Use PHP to create an ADO recordset with SQL Server

This is an example on how to connect to MSSQL server with ADODB and PHP.

NOTE: your php.ini must contain this line:

[COM_DOT_NET] 
extension=php_com_dotnet.dll


To connect do the following

$myServer = "localhost";
$myUser = "MyUser";
$myPass = "MyPass"; 
$myDB= "MyDatabase"; 
$conn = new COM ("ADODB.Connection")
or die("Cannot start ADO");
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
$conn->open($connStr);

$select = "SELECT * FROM MyDatabaseTable";
$rs = $conn->execute($select);
if(!$rs->EOF){
   while(!$rs->EOF){
      echo $rs->Fields['MyField']->value."<br>";
      $rs->MoveNext();
   }
}
$rs->Close();
$rs = null;

$conn->Close();
$conn = null; 



NOTE:   if you want to do an insert statement, you open the connection in the same way as shown above, and then you do this:


$strSQL = "insert into icb_weblogins (userid) values ('fred@icbconsulting.com')";
$conn->Execute($strSQL);  





Comments