PHP also provide the way connect to Component Object Model (COM) that is used wildly on MSWindow platform. The following code will show you how to connect to database via COM. You can check for the built-in function provided by php at the below url:
http://www.php.net/manual/en/ref.com.php
Build a connection
Make PHP script connect to SQL Server using COM
<?php $conn = new COM("ADODB.Connection");
$recordset = new COM("ADODB.RecordSet");
$conn->open("Driver={SQL Server};Server=localhost;Database=db_name; Uid=username;Pwd=password"); ?>
You will notice that the open() function is belong to ADODB.Connection object provide by Microsoft. That means the $conn variable is able to access any function in ADODB.Connection. Also, the $recordset variable is able to access any function in ADODB.RecordSet.
Retrieve data from database
//Query the database $sql = "SELECT username FROM users WHERE username='username' and password='password'"; $recordset->open($sql, $conn, 3); if(!$recordset->EOF){
$recordset->movefirst(); $username = $recordset->fields["username"]->value; }
Add new record
To addnew record, just create sql insert statement and then call the execute() function as below:
$sql = "INSERT INTO users (username, password) VALUES('Ruby', 'r0012')";
$conn->execute($sql);
Update record
To update record, just create sql update statement and then call the execute() function as below:
$sql = "UPDATE users SET password='rNewPwd' WHERE username='Ruby'"; $conn->execute($sql);
Delete record
To delete record, just create sql delete statement and then call the execute() function as below:
$sql = "DELETE FROM users WHERE username='Ruby'"; $conn->execute($sql);
Close connection
Don't forget to close recordset and then close the connection.
$recordset->close();
$conn->close();