Beginning PHP and MySQL For Dynamic Web Pages

0

PHP or PHP Hypertext Preprocessor is a very powerful open source server side scripting language used for creating dynamic web pages. A Server side scripting language is a language that is read on the server side and not the client side. When a user accesses a web site written in PHP the web server will read the page and output HTML to the users web browser. HTML is different than PHP in that HTML is a client side language, when a user requests an HTML page the web server simply send the text in the HTML page to the users browser which then formats that page based on the HTML. MySQL is also a free open source software the is used to create databases that are used primarily by web servers. The My stands for the name of the co-founders daughter, My while the SQL stands for structured Query Language.

Mixing PHP and MySQL together in a website will give you unlimited possibilities of what you can produce. One little problem with developing websites in PHP is that since it is a server side scripting language you have to open the page from a web server to view it properly. Unlike HTML where you can save an HTML document on your desktop and then view it from there, PHP needs that server interaction to view properly. The good news is most web hosting companies provide PHP and MySQL for a pretty cheap cost to the users.

So think of the possibilities that we have by using a dynamic scripting language like PHP and a stable open source database like MySQL. When I first learned 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 pages. So in terms of updating a Web Site design using PHP and MySQL would be a huge time saver.

With most web hosting where you have your hosting and MySQL databases together it makes it very easy to connect to the MySQL database to extract information or even insert information. So lets say that we have a database called “mysite” and the table in “mysite” called “mypages” and it is formated like the following:

ID | Title | Page |

So we have three fields in the table “mypages” which is in the database “mysite” and currently we do not have anything in this table it is just an empty table. So now we want to add something to this table using PHP and we will start at the beginning by connecting to the database and the table so we can INSERT information into the table.

First to connect to a MySQL database server we need to use the mysql_connection statement and give the server all the required information:

mysql_connect(server address, username, password);

Now we are going to save this statement to a variable so that we can use it over and over again.

$conn = mysql_connect(‘localhost’, ‘mike’, ‘mikenetpc’);

Next we need to select the database within the server.

mysql_select_db(‘mysite’);

So we have successfully connected to our database server and we have successfully selected the database we want to use. Lets now add some information to the table ‘mypages’ within the database ‘mysite’. To do this we are going to use the mysql_query command along with the SQL INSERT command.

mysql_query(“INSERT INTO mypages (ID, Title, Page) VALUES (1, ‘Home Page’, “This is my home page, Welcome’)”);

Now we have inserted a single row into the table ‘mypages’ lets add another one.

mysql_query(“INSERT INTO mypages (ID, Title, Page) VALUES(2, ‘About Page’, ‘ This page is about me’)”);

We have successfully added two rows of information into our table but what good is this information if we can get it out of the table to display on a website. To extract the information from the table we will pull out all of the information and then create a loop to display it all. This is actually a lot easier than it my sound because we are going to extract the information we want into an array for easier handling. To get data from the table we will use the mysql_query command again and the SELECT command. I will write the entire piece of code and then I will explain it a little further down.

$result = mysql_query(“SELECT * FROM mypages);

while($row = mysql_fetch_array($result))

{

echo $row[‘ID’].”
“;

echo $row[‘Title’].”
“;

echo $row[‘Page’].”
“;

echo “
;

}

We have already explained the first line, it SELECTS everything from the ‘mypages’ table of the database and places it into an array called $results. Next the create a while loop that will continue until we have reached the end of the array. Each time the loop occurs we move forward another line in the array so we will be displaying everything. Next we use the command echo which tells the server to display the information to the browser. So the statement echo $row[‘ID’].”
“; will display the ID number of where we are in the array and then will display a
command to the web browser which is line break. The output of the above code would look like this.

1

Home Page

This is my home page, Welcom

2

About Page

This page is about me

So we have now successfully written to our database and we have successfully pulled information from our database. Those simple commands are the very basics of PHP and MySQL. Each of this commands has quite a few more attributes to them that you can use to sort all the information the table or even just pull out a single record from the table.

Source

Leave A Reply
Bitcoin (BTC) RM431,546.77
Ethereum (ETH) RM16,526.84
Tether (USDT) RM4.46
BNB (BNB) RM3,437.77
USDC (USDC) RM4.46
XRP (XRP) RM11.82
BUSD (BUSD) RM4.28
Cardano (ADA) RM5.49
Solana (SOL) RM1,051.40
Dogecoin (DOGE) RM1.87
Polkadot (DOT) RM45.09
Polygon (MATIC) RM3.21
Lido Staked Ether (STETH) RM16,527.46
Shiba Inu (SHIB) RM0.000136
Dai (DAI) RM4.46
TRON (TRX) RM1.73
Avalanche (AVAX) RM243.18