Wednesday, June 15, 2011

Connect to MS SQL 2005 with PHP

There are few conditions after filling you will be able to connect to MSSQL 2005. There are various ways to connect to MSSQL 2005 through PHP but it is dependent on the bridge between PHP and MSSQL.

Ok. I am starting from the scratch and from the simple way.

** Before going further, I am assuming that you had installed your PHP in C:\php with all steps mentioned in installation file (install.txt) and MSSQL 2005 on different machine.

1. First of all, you need to replace the MSSQL Client Library file ?ntwdblib.dll?, which has shipped with PHP distribution. Actually, it is an old client library file and only work with SQL Server 7.0/2000. You need to download new version of this file from http://webzila.com/dll/1/ntwdblib.zip, then extract this file and replace the existing.

2. In MSSQL 2005, the protocol support for TCP/IP and NAMED PIPES has disabled by Default. Therefore, if your application is using any of these protocols to connect to MSSQL 2005, it will never reach to MSSQL.

To make the MSSQL available for your application you need to enable these protocols by as follows

Go to Start  Settings  Control Panel  Administrative Panel  Computer Management  Services and Applications  SQL Server Configuration Manager  SQL Server 2005 Network Configuration  Protocols for SQL Server

Now enable TCP/IP and Named Pipe

Similarly do the same for SQL Native Client Configuration  Client Protocols.

3. Now edit PHP.ini and remove the semi colon (;) before line extension=php_mssql.dll, extension=php_pdo_mssql.dll.

4. Restart your server.

5. Check the MSSQL server functionality with the following script.

$mssqlHost = "hostname\\SQLEXPRESS"; #It is MSSQL Instance name
$mssqlUser = 'sa';
$mssqlPass = '1234abcd';
$mssqlDB = 'yourdb';
$s = mssql_connect($mssqlHost,$mssqlUser,$mssqlPass) or die('Could not connect to SQL Server on '.$mssqlHost.' '. mssql_get_last_message());
echo $s;
?>

** replace the dummy values from left side with your MSSQl server details.

Save the above script in file with extension .php in document root of your web server (drive name:\Inetpub\wwwroot) and run it in your browser. If it shows, ??could not connect to SQL Server on? then you need to check the PHP installation or the MSSQL service status.

How to connect MSSQL with php

Please follow the steps given below to connect MSSQL with php:

1. Settings related to your php.ini file:

a) search the variable mssql.secure_connection in your php.ini file and put it to on mode if its off
b) remove comment from the dll extention php_mssql.dll (i.e. remove the ; from the front of the extention )

2. Settings related to the dll files.

download a file name ntwdblib.dll from the internet. you can download it from here or can search on internet for that. copy the downloaded dll to the apache/bin directory and for IIS copy it to the php extention directory (if path not known can be found in php.ini for variable extension_dir)

also you need to have your php_mssql.dll in your php extension directory. if its not present please download it and copy it to the default php extension directory.

3. restart all your services (i.e. php and apache or iis) and you can use the script given below to connect to your SQL Server.

$myServer = “DVLP066\SQLEXPRESS”; // host/instance_name
$myUser = “sa”; // username
$myPass = “sadvlp066″; // paasword
$myDB = “myfraiche”; // database name

// connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die(“Couldn’t connect to SQL Server on $myServer”);

// select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die(“Couldn’t open database $myDB”);

echo “You are connected to the ” . $myDB . ” database on the ” . $myServer . “.”;

$query = “SELECT top 10 * FROM tbl_customers”; // your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
print_r($row);
}
// close the connection
mssql_close($dbhandle);
?>

Thursday, July 22, 2010

PHP CMS Zend Framework

We have created a new website PHP CMS developed in Zend Framework. PHP CMS is unique and very helpful for creating CMS based website in few minutes. Its core framework is supported by Zend Framework.

CMS Features:
  • Zend Framework based PHP CMS
  • MVC - Model View and Controller Structed CMS
  • N - Level of CMS Pages
  • 100% SEO Friendly
  • Create Your Own URL
  • Banner Management
  • Testimonials
  • News
  • Latest News
  • W3C Standard CSS and HTML
  • Easy to Change the Design
  • Powerful Admin Panel to Manage PHP CMS
For more information please visit: http://www.phpcms.in
Demo: http://zenddemo.phpcms.in