Beginning PHP and MySQL For Dynamic Web Pages

PHP or PHP Hypertext Preprocessor is a verySo we have successfully connected to our database
powerful open source server side scripting languageserver and we have successfully selected the
used for creating dynamic web pages. A Server sidedatabase we want to use. Lets now add some
scripting language is a language that is read on theinformation to the table 'mypages' within the
server side and not the client side. When a userdatabase 'mysite'. To do this we are going to use the
accesses a web site written in PHP the web servermysql_query command along with the SQL INSERT
will read the page and output HTML to the userscommand.mysql_query("INSERT INTO mypages (ID,
web browser. HTML is different than PHP in thatTitle, Page) VALUES (1, 'Home Page', "This is my
HTML is a client side language, when a user requestshome page, Welcome')");
an HTML page the web server simply send the textNow we have inserted a single row into the table
in the HTML page to the users browser which then'mypages' lets add another one.mysql_query("INSERT
formats that page based on the HTML. MySQL isINTO mypages (ID, Title, Page) VALUES(2, 'About
also a free open source software the is used toPage', ' This page is about me')");
create databases that are used primarily by webWe have successfully added two rows of
servers. The My stands for the name of theinformation into our table but what good is this
co-founders daughter, My while the SQL stands forinformation if we can get it out of the table to
structured Query Language.display on a website. To extract the information
Mixing PHP and MySQL together in a website will givefrom the table we will pull out all of the information
you unlimited possibilities of what you can produce.and then create a loop to display it all. This is actually
One little problem with developing websites in PHP isa lot easier than it my sound because we are going
that since it is a server side scripting language youto extract the information we want into an array for
have to open the page from a web server to view iteasier handling. To get data from the table we will
properly. Unlike HTML where you can save an HTMLuse the mysql_query command again and the
document on your desktop and then view it fromSELECT command. I will write the entire piece of
there, PHP needs that server interaction to viewcode and then I will explain it a little further down.
properly. The good news is most web hosting$result = mysql_query("SELECT * FROM
companies provide PHP and MySQL for a prettymypages);while($row = mysql_fetch_array($result))
cheap cost to the users.{echo $row['ID']."
So think of the possibilities that we have by using a";echo $row['Title']."
dynamic scripting language like PHP and a stable open";echo $row['Page']."
source database like MySQL. When I first learned";echo "
PHP and MySQL my first thought was that by using;
PHP and a MySQL I could create a website that could}
contain thousands of pages but only three actualWe have already explained the first line, it SELECTS
pages. So in terms of updating a Web Site designeverything from the 'mypages' table of the database
using PHP and MySQL would be a huge time saver.and places it into an array called $results. Next the
With most web hosting where you have yourcreate a while loop that will continue until we have
hosting and MySQL databases together it makes itreached the end of the array. Each time the loop
very easy to connect to the MySQL database tooccurs we move forward another line in the array so
extract information or even insert information. So letswe will be displaying everything. Next we use the
say that we have a database called "mysite" and thecommand echo which tells the server to display the
table in "mysite" called "mypages" and it is formatedinformation to the browser. So the statement echo
like the following:$row['ID']."
ID | Title | Page |"; will display the ID number of where we are in the
So we have three fields in the table "mypages" whicharray and then will display acommand to the web
is in the database "mysite" and currently we do notbrowser which is line break. The output of the above
have anything in this table it is just an empty table.code would look like this.
So now we want to add something to this table1
using PHP and we will start at the beginning byHome Page
connecting to the database and the table so we canThis is my home page, Welcom
INSERT information into the table.2
First to connect to a MySQL database server weAbout Page
need to use the mysql_connection statement andThis page is about me
give the server all the requiredSo we have now successfully written to our
information:mysql_connect(server address, username,database and we have successfully pulled information
password);from our database. Those simple commands are the
Now we are going to save this statment to avery basics of PHP and MySQL. Each of this
variable so that we can use it over and over again.commands has quite a few more attributes to them
$conn = mysql_connect('localhost', 'mike', 'mikenetpc');that you can use to sort all the information the table
Next we need to select the database within theor even just pull out a single record from the table.
server.mysql_select_db('mysite');