Ever wondered using PHP and your coming from a Microsoft background? I guess the transition is not that hard specially when you had used the classic ASP, and whats good most of the tools you use for development is 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 dont like editing configuration of Apache directly on notepad you can download ApacheConf Lite so you have a GUI like this
- 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
Thats it and you can start configuring your Apache Server by editing the conf file at C:{your XAMPP Root Directory}xamppapacheconfhttpd.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.
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}xamppphpext” folder and add a reference to your PHP.ini file which can be found in “C:{your XAMPP Root Directory}xamppphp” folder, you can do that by going to the “Dynamic Extensions” sections and add the line
extension=php_sqlsrv_53_ts_vc6.dll
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 checl 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 /*Connect using SQL Server Authentication.*/ $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') { /*Insert data.*/ $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>"; } } } /*Display Data*/ $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?
Hi, rsmacaalay.
Thanks for the tutorial, it is very useful. XD
I have a question, why if I used this code in a single file, it’s works fine, but when I copy it and paste in another file with html , it doesn’t work, ¿¿¿Why it’s happend ??? I suspect that I have a problem with the DIV that I use to define tabs in a hatml page.
thanks for your answer
Best regards
can you send me the code you copy and pasted