| PHP or PHP Hypertext Preprocessor is a very | | | | So we have successfully connected to our database |
| powerful open source server side scripting language | | | | server and we have successfully selected the |
| used for creating dynamic web pages. A Server side | | | | database we want to use. Lets now add some |
| scripting language is a language that is read on the | | | | information to the table 'mypages' within the |
| server side and not the client side. When a user | | | | database 'mysite'. To do this we are going to use the |
| accesses a web site written in PHP the web server | | | | mysql_query command along with the SQL INSERT |
| will read the page and output HTML to the users | | | | command.mysql_query("INSERT INTO mypages (ID, |
| web browser. HTML is different than PHP in that | | | | Title, Page) VALUES (1, 'Home Page', "This is my |
| HTML is a client side language, when a user requests | | | | home page, Welcome')"); |
| an HTML page the web server simply send the text | | | | Now 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 is | | | | INTO mypages (ID, Title, Page) VALUES(2, 'About |
| also a free open source software the is used to | | | | Page', ' This page is about me')"); |
| create databases that are used primarily by web | | | | We have successfully added two rows of |
| servers. The My stands for the name of the | | | | information into our table but what good is this |
| co-founders daughter, My while the SQL stands for | | | | information 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 give | | | | from 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 is | | | | a lot easier than it my sound because we are going |
| that since it is a server side scripting language you | | | | to extract the information we want into an array for |
| have to open the page from a web server to view it | | | | easier handling. To get data from the table we will |
| properly. Unlike HTML where you can save an HTML | | | | use the mysql_query command again and the |
| document on your desktop and then view it from | | | | SELECT command. I will write the entire piece of |
| there, PHP needs that server interaction to view | | | | code 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 pretty | | | | mypages);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 actual | | | | We have already explained the first line, it SELECTS |
| pages. So in terms of updating a Web Site design | | | | everything 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 your | | | | create a while loop that will continue until we have |
| hosting and MySQL databases together it makes it | | | | reached the end of the array. Each time the loop |
| very easy to connect to the MySQL database to | | | | occurs we move forward another line in the array so |
| extract information or even insert information. So lets | | | | we will be displaying everything. Next we use the |
| say that we have a database called "mysite" and the | | | | command echo which tells the server to display the |
| table in "mysite" called "mypages" and it is formated | | | | information 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" which | | | | array and then will display acommand to the web |
| is in the database "mysite" and currently we do not | | | | browser 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 table | | | | 1 |
| using PHP and we will start at the beginning by | | | | Home Page |
| connecting to the database and the table so we can | | | | This is my home page, Welcom |
| INSERT information into the table. | | | | 2 |
| First to connect to a MySQL database server we | | | | About Page |
| need to use the mysql_connection statement and | | | | This page is about me |
| give the server all the required | | | | So 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 a | | | | very 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 the | | | | or even just pull out a single record from the table. |
| server.mysql_select_db('mysite'); | | | | |