Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

A Beginner's Guide to PHP

2.33/5 (3 votes)
23 Mar 2010CPOL2 min read 1  
This is a beginner's guide to PHP

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?

  1. Apache Server (you can download XAMPP, so you have the complete toolset)
  2. If you don't like editing configuration of Apache directly on Notepad, you can download ApacheConf Lite so you have a GUI like this:

    Image 1

    Apache Conf Lite
  3. Text Editor (I use Dreamweaver, but you can use Netbeans or even Notepad)
  4. 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.

Image 2

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

Image 3

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
<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?

Image 4 Image 5 Image 6 Image 7 Image 8 Image 9

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)