Ever wondered about using PHP and you are coming from a Microsoft background? I guess the transition is not that hard especially when you had used the classic ASP, and what's good, most of the tools you use for development are free. Also, now you can run PHP on IIS but for the purposes of this post, I will run it under Apache Server in Windows 7.
So what do I need?
- Apache Server (you can download XAMPP, so you have the complete toolset)
- If you don't like editing configuration of Apache directly on Notepad, you can download ApacheConf Lite so you have a GUI like this:
Apache Conf Lite
- Text Editor (I use Dreamweaver, but you can use Netbeans or even Notepad)
- Since I will be using SQL Server as my database, I need the SQL Server Driver for PHP which can be downloaded here.
That's it and you can start configuring your Apache Server by editing the conf file at C:\{your XAMPP Root Directory}\xampp\apache\conf\httpd.conf or an easier way would be installing the XAMPP. Once done, make sure that your IIS is disabled as it would conflict in using port 80. Now you can fire up the XAMPP Control Panel or the Apache Command Line.
XAMPP Control Panel
Now you will notice that you also have MySql, FileZilla and Mercury but will not be using that for now.
Before starting the Apache Server, you also need to install/extract the SQL Server Driver for PHP in the “C:\{your XAMPP Root Directory}\xampp\php\ext” folder and add a reference to your PHP.ini file which can be found in “C:\{your XAMPP Root Directory}\xampp\php\” folder. You can do that by going to the “Dynamic Extensions” sections and add the line:
extension=php_sqlsrv_53_ts_vc6.dll
PHP INI file
Now you can start the server by running this command:
apachectl start
Or clicking the start on the XAMPP Control Panel. You can also install it as a service when you tick the SVC check box.
Now you can start coding.
Here is my sample code for accessing a SQL Server Database:
<html>
<head>
<Title>Example Web Form</Title>
</head>
<body>
<form method="post" action="?action=add" enctype="multipart/form-data" >
Name<input type="text" name="Name" id="Name"/></br></br>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$server = "MYSQLSERVER01";
$user = "myuser";
$pass = "mypassword";
$database = "MyDatabase";
$connectionoptions = array("Database" => $database,
"UID" => $user,
"PWD" => $pass,
"MultipleActiveResultSets" => false);
$conn = sqlsrv_connect($server, $connectionoptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
if(isset($_GET['action']))
{
if($_GET['action'] == 'add')
{
$insertSql = "INSERT INTO SampleTable (Name) VALUES (?)";
$params = array(&$_POST['Name']);
$stmt = sqlsrv_query($conn, $insertSql, $params);
if($stmt === false)
{
die(print_r($errors, true));
}
else
{
echo "Insert Successful</br>";
}
}
}
$sql = "SELECT * FROM SampleTable ORDER BY UserID DESC";
$stmt3 = sqlsrv_query($conn, $sql);
if($stmt3 === false)
{
die(print_r(sqlsrv_errors(), true));
}
if(sqlsrv_has_rows($stmt3))
{
print("<table border='1px'>");
print("<tr><td>Name</td>");
print("<td>User ID</td></tr>");
while($row = sqlsrv_fetch_array($stmt3))
{
print("<tr><td>".$row['Name']."</td>");
print("<td>".$row['UserId']."</td></tr>");
}
print("</table>");
}
?>
</body>
</html>
Once done, save this as a Default.php and put it in your Apache web root directory! Isn’t that simple?