Professional Documents
Culture Documents
Installing Configuring Developing With X Am PP
Installing Configuring Developing With X Am PP
DISCLAIMER: A lot of care has been taken in the accuracy of information provided in this article ho!ever it cannot be "uaranteed that inaccuracies !ill not occur# $he author and Skills Canada !ill not be held responsible for any claims or dama"es caused as a result of any information from this article#
$his article e%plains the installation and confi"uration process of the &AM'' Apache distribution and then provides a step(by(step tutorial on developin" a simple address book pro"ram usin" the ')' pro"rammin" lan"ua"e and MyS*L database packa"e# In order to comply !ith the +ntario Skills Competition this article !ill use the follo!in" codin" standards and tools: &)$ML ,#- $ransitional ')' .#- MyS*L .#- and phpMyAdmin /#0#
:e%t !e !ill test if the server is runnin" correctly by openin" an internet bro!ser and typin" http://localhost/ into the address bar# If confi"ured correctly you !ill be presented !ith a screen similar to that of the one in 3I;9RE ,#
In order to stop all Apache processes do not close the runnin" terminal application but instead run another batch file in the xampplite directory called apache_stop.bat#
$he above code e%ample starts a ne! &)$ML document !ith the pa"e title set as <)ello 2orld= and then prints a sin"le line of te%t Hello World usin" the echo ')' lan"ua"e construct# In order to run and test this ')' pro"ram save it into your xampplite\htdocs directory !ith the file name of helloWorld.php# $hen to vie! it in your bro!ser simply type in http://localhost/helloWorld.php into the address bar# If done correctly you !ill see your Hello World line of te%t in the !eb bro!ser as sho!n in 3I;9RE /#
$he &AM'' Lite packa"e contains an application called phpMyAdmin !hich allo!s developers to administer and maintain MyS*L databases# 3or the remainder of this article !e !ill be usin" phpMyAdmin to create a database and table and enter test data used in the address book pro"ram# In order to start phpMyAdmin you must first copy the phpMy dmin folder from the xampplite directory and place it into the xampplite\htdocs directory# 4efore testin" phpMyAdmin make sure that both Apache and MyS*L are runnin" by openin" their respective batch files: apache_start.bat and mysql_start.bat# Alon" !ith Apache and MyS*L runnin" in the back"round type http://localhost/phpMy dmin/ into your internet bro!ser# If successful you !ill be presented !ith a phpMyAdmin start pa"e similar to the one in 3I;9RE ?#
$he first step !ith phpMyAdmin runnin" is creatin" a ne! database# $o do this point to the te%tbo% labelled Create new database and type address!oo" as the name of a ne! database# 9pon doin" so you !ill be for!arded to a screen similar to the one in 3I;9RE @# :e%t insert a ne! table in the address!oo" database by typin" colleag#e into the table name te%tbo% and clickin" the $o button to proceed !ith creatin" the table#
3ollo!in" the creation of the colleag#e table you !ill be re5uired to insert the columns that make up your table# 4y usin" $A4LE / as a reference insert the necessary columns that !ill make up your colleag#e table for the address book#
Lastly !ith the database and table created !e ne%t need to input some data into the table# 9sin" the fictional information from $A4LE ? on the ne%t pa"e input the contact data into your o!n table#
:+$E: 2hen inputtin" data into the table leave the id field blank because it is set to auto increment to an automatic number on every record entry#
2e are no! ready to !rite the ')' pro"ram that !ill connect to the database and display our data in an internet bro!ser# $he procedure that !e !ill !rite this pro"ram is to first establish a connection !ith the database usin" ')' then start our &)$ML deceleration and meta data and finally in the body of our pa"e !rite a loop that !ill iterate throu"h each record in the colleag#e table and display that data in the bro!ser#
$he ')' block above !ill connect to our database usin" localhost as the address root as the username and a blank pass!ord field# $his confi"uration of root as the username and a blank pass!ord is the default that is set !hen phpMyAdmin is confi"ured and is !hat !e !ill be usin" for our e%amples#
:+$E: Althou"h it is perfectly fine to use the default username and pass!ord for learnin" purposes it is stron"ly su""ested to chan"e the pass!ord to somethin" more secure if developin" full !eb applications#
3ollo!in" the mysql_connect%&localhost&' &root&' &&() line of code is another line statin" that !e !ill be connectin" to and usin" the address!oo" database# Lastly !e !ill create a variable called res#lt !hich !ill contain a S*L 5uery that !ill select all records from the colleag#e table#
In the above code e%ample you !ill notice that the ')' code block contained !ithin *+php and +, is started and then ended before declarin" the &)$ML document# Remember that in the <)ello 2orld= e%ample earlier in this article !e also had a ')' code block !ithin a &)$ML para"raph ta"# $his is important to note because &)$ML and ')' can interchan"eably be used !ithin one another# $his concept !ill also be sho!n !hen !e !rite a ')' loop to retrieve all the records from the database and display it in a &)$ML table# Insert a couple ne! lines after the closin" */head, ta" and then type in the follo!in" code:
$his ne! addition to your code !ill simply start the body of your document and then place a ne! &)$ML table alon" !ith a ne! table ro! follo!ed by five table headin"s: ID 3irst :ame Last :ame $elephone and Email Address#
&
:e%t !e !ill create a ne! ')' code block in !hich !e !rite a while loop that "oes throu"h each database table record and outputs each piece of data in a ne! &)$ML table cell:
$his loop !ill !rite a ne! table ro! for each record by printin" the *tr, ta" and then also !rite the id first-ame last-ame telephone and email into its o!n respective table cells usin" the *td, ta"s# 3inally before endin" the loop the table ro! is closed by printin" out */tr,# $he ')' code block is concluded by freein" the memory !ith mysql_free_res#lt%.res#lt() and closin" the database connection !ith mysql_close%()# $he maAority of the code has no! been !ritten ho!ever to properly end this &)$ML document !e !ill need to insert a fe! more closin" ta"s:
Bou may refer to the ne%t pa"e for complete code of the address book pro"ram#
:o! that you have created a database and table inserted some data into that table and !ritten a pro"ram that reads the data usin" ')' all that is left to do is test and if re5uired debu"# Makin" sure that the Apache )$$' Server alon" !ith MyS*L are runnin" open up an instance of your internet bro!ser and type in the location of your address book ')' file# 3or e%ample if you created an address!oo" folder inside the htdocs folder then the path to run the address book pro"ram !ould be: http://localhost/address!oo"/index.php# If done correctly you !ill be presented !ith a pa"e similar to that of the one in 3I;9RE .# $his fi"ure sho!s the address book pro"ram runnin" !ith a &)$ML table sho!in" the data from the colleag#e table# If you encounter an error please revie! and compare your code !ith the complete code provided on the previous pa"e# Lastly to stop and close all processes run the apache_stop.bat and mysql_stop.bat batch files for the server and database respectively#
3I;9RE .: $he address book pro"ram displayin" contact information from the database#
Conclusion
$hrou"hout this article you have been sho!n the be"innin" concepts that !ill allo! you to set up a development environment create and administer a database and !rite a pro"ram that connects to a database and retrieves data# Althou"h these concepts are important ones they are in no means the end of an application# 4y usin" the address book pro"ram as an e%ample there is still a lot of features that can be implemented in such an application# 3eatures such as editin" contact information deletin" contacts addin" ne! contacts and bein" able to email contacts throu"h a form are all additions that can be implemented to the e%ample used in this article# Challen"e yourself use this article and other resources as a Aump(start and try addin" other capabilities to your o!n address book pro"ramC
+%