Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!



More PHP Basics, Part 2


P.G. Daly

3/26/2007

Printer Friendly Version

The development platform PHP continues to grow in popularity, becoming more of a powerhouse in 2007. Two new recent releases of PHP drive home the point that there is constant care and feeding of this platform by the open source community as usage continues to grow.

In February, version 5.2.1 was released to offer enhanced security and bug fixes to the version 5 series. Most recently in March, version 4.4.6 was released to solve a crash problem from the previous version in the version 4 series release.

For the non-programmer, PHP (Hypertext Preprocessor) is an open source, server-side, HTML embedded scripting language used to create dynamic Web pages. PHP script is embedded within an HTML document, and the programmer can jump between HTML and PHP seamlessly within the document while the code remains inaccessible to users because it is processed on the server. It combines the power of CGI scripting with the ease of working within HTML.

Here's part two of a continuing series on PHP Basics.

Interacting with mySQL

The most powerful part of any dynamic programming language is its ability to interact with a database. PHP works almost effortlessly with the mySQL database (define). For this example, let's assume you already have created a table called "Employees" with the following fields:

FirstName
LastName
Gender
Department
And have it populated with the following data:

Data
First Name Last Name Gender Department
Sally Jones Female Accounting
Joe Smith Male Marketing
Tracy Toms Female Procurement

The URL where your database is hosted is my.company.com, the Database Name is "CompanyData," the username is "admin," and the password is "getmein."

More From Intranet Journal

PHP Basics, Revised

Creating a PHP-Based Content Management System

PHP Basics

If you want to comment on these or any other articles you see on Intranet Journal, we'd like to hear from you in our IT Management Forum. Thanks for reading.

- Tom Dunlap, Managing Editor.

FREE IT Management Newsletters

The first thing you need to do in PHP is establish a connection to the database. You can do that with this code:

<?php
 // Connects to the Employee Database 
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error()); 
mysql_select_db("CompanyData") or die(mysql_error()); 
?>

Retrieving Data from the table requires that you retrieve the data, store it in a variable, and then print it out. To do this for "x" number of rows in a database, you need to put it into a loop so that it executes multiple times (otherwise you get the first and only the first row in your results). This big code block would look something like:

<?php
 // Connects to the Employee Database 
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error()); 
mysql_select_db("CompanyData") or die(mysql_error()); 
?>

<?php
// Connects to the Employee Database 
mysql_connect("my.company.com", "admin", "getmein") or die(mysql_error()); 
mysql_select_db("CompanyData") or die(mysql_error()); 

// Create a SQL query to select all records from the database and store it in the variable data

$data = mysql_query("SELECT * FROM Employees") 
or die(mysql_error()); 

// Print a table with headings in which to display the results
Print "<table border cellpadding=3>";
Print "<tr>"; 
Print "<th>First Name:</th>"; 
Print "<th>Last Name:</th>"
Print "<th>Gender:</th>"
Print "<th>Department:</th></tr>"


// Start the loop that says while there is something in the SQL results, keep performing these instructions
 
while($info = mysql_fetch_array( $data )) 
{ 
Print "<tr>"; 
Print "<td>".$info['FirstName'] . "</td> "; 
Print "<td>".$info['LastName'] . " </td>
Print "<td>".$info['Gender'] . " </td>
Print "<td>".$info['Department'] . " </td></tr>"; 
} 

//Close out the table code when all the data is done
Print "</table>"; 
?>

The comments I put in the code are pretty self explanatory. The only real missing piece is the

mysql_fetch_array($data)
section. What that does is fetch the SQL results which were stored in the $data variable and put them into an array.

Needless to say you can expand on the statement

$data = mysql_query("SELECT * FROM Employees") 
or die(mysql_error()); 
To create much more complex queries that use "where" statements to limit results to specific criteria which you can either hardcode in your SQL statement or obtain from a web form.

Hope these additional code snippets and understanding help you along your way into PHP. It is most definitely here to stay and is a worthwhile addition to your programming toolkit.

Printer Friendly Version


Other Resources
from Intranet Journal
  • Intranet Journal Discussion Forum
  • Hasta La Vista, Windows
  • Salesforce.com Offers Invitation-Only Collaboration
  • Ten Leading Open Source Innovators
  • from JupiterWeb

    email this page

    Tutorials
    and more at:
    Intranet Journal's Tutorials
    Intranet Journal Favorites

    Creating a PHP-Based Content Management System

    The Spyware Guide

    Introduction to Microsoft SharePoint Portal

    Intranet Journal
    Part of the EarthWeb Network

    Managing Editor
    Intranet Journal

    Tom Dunlap

    EarthWeb Home Page
    Jupitermedia Home Page

    Media Kit




    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers