Nov 1, 2010

Connect to MSSQL Server Database using PHP

Connect to MSSQL  Server Database using PHP


This lesson wont work if your php version is 5.3 due to PHP dropped support in 5.3 for the mssql.dll.




Other solutions if your using a 5.3 version:
Otherwise, if your not using 5.3 version, follow my instructions below:

  1. Download and install Microsoft SQL Server 2005 Express Edition here. Or you can download the 2008 version of you want.
  2. Download and install SQL Server Management Studio here.
  3. If you installed successfully both products on your computer, go to start -> all programs -> microsoft sql server -> configuration tools -> sql server surface area configuration. Then, go to Service Area Configuration for Services and Connections -> Remote Connections. Finally, choose Local and Remote Connections -> use both tcp/ip and named pipes.
  4. Set up the authentication mode. Open sql server management studio express -> on the root folder, right click -> choose properties. Finally, select the security page -> choose sql server and windows authentication mode. Click OK.
  5. Create an SQL user. Open sql server management studio express -> click the root folder -> security folder, right click -> choose new -> login. Finally, create a username and choose sql server authentication, create password. Press OK.
  6. Close sql server management studio express. Restart your MS SQL server by opening sql server configuration manager -> right click on sql server(SQLEXPRESS) -> Restart.
  7. After restart success. Close sql server configuration manager.
  8. Open sql server management studio express by accessing using SQL Server authentication. use the account details you created in step 5.
  9. After logging in. create now the database. ( I think you already know how to set this up. )
  10. Then, configure your php.ini and enable extension=php_mssql.dll and extension=php_pdo.dll by removing the ";" before it. Restart Apache.
  11. Finally, create the connection on your php file. I have my example below.

if ($connection = mssql_pconnect('hostname\SQLEXPRESS', 'username',
'password')) {

echo 'Connected.';

} else {
echo 'Connection failed.';
}


hostname\SQLEXPRESS - this is the Server Name
username - the username you use to create the database.
password - the password you create using the database.

To use php mssql, you can do further reading here.


At last, if you face any problem just leave a message.

Cheers

No comments: