Start coding

Section 3. Start coding

Your first script
Following tradition, we will begin coding with a “hello world” example. Fire up your text
editor and type the following code:

<HTML>
<?php
echo "Hello World";
?>
</HTML>

Save the file as first.php and view it in the browser (remember to set the permission to
chmod 775 first). The page shows “Hello World”. View the HTML source of this page
through your browser. You will only see the text Hello World. This happened
because PHP processed the code, and the code told PHP to output the string “Hello
World”. Notice the <?php and ?>. These are delimiters and enclose a block of PHP
code. <?php tells PHP to process all the lines following this as PHP code and ?> tells
PHP to stop processing. All lines beyond this scope are passed as HTML to the
browser.
Your first database
Now that we have PHP running properly and have created our first script, let’s create our first
database and see what we can do with it. Drop to console and type in the following
command:
mysqladmin -uroot create learndb
This creates a database named “learndb” for us to use. Here we have assumed that you are
root user. If you are logged in as another user, just use the command mysqladmin
-uusername -pyourpassword create learndb, replacing username and
yourpassword with your username and password respectively. If you are hosting your site
through a hosting company, you probably don’t have permission to run mysqladmin. In this
case, you have to ask your server administrator to create the database for you.
Next we will create tables in this database and enter some information. Go to the console.
Type:
mysql
You should see something like:
Welcome to MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5 to server version 3.22.34
Type ‘help’ for help.
Type:

CONNECT learndb
CREATE TABLE personnel
(
id int NOT NULL AUTO_INCREMENT,
firstname varchar(25),
lastname varchar(20),
nick varchar(12),
email varchar(35),
salary int,
PRIMARY KEY (id),
UNIQUE id (id)
);
INSERT INTO personnel VALUES ('1','John','Lever','John', 'john@everywhere.net','75000');
INSERT INTO personnel VALUES ('2','Camilla','Anderson','Rose', 'rose@flower.com','66000');

This creates a table with 5 fields and puts some information in it.

Where’s my view?

Now that we have a database with some information with it, let’s see if we can view it with
PHP. Save the following text as viewdb.php:

<HTML>
<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel",$db);
echo "<TABLE>";
echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Salary</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<TR><TD>";
echo $myrow["firstname"];
echo " ";
echo $myrow["lastname"];
echo "<TD>";
echo $myrow["nick"];
echo "<TD>";
echo $myrow["salary"];
}
echo "</TABLE>";
?>
</HTML>

Run it through your browser and you will see a personnel database. But what is this code
doing and how is it generated? Let’s examine the code. First we declare a variable $db. In PHP we declare a variable by putting the ‘$’ sign before it. The string after $ is the name of
that variable. We assign value to it by coding:$variable_name=somevalue; (example:
$count=4;)
Remember to put ‘;’ after all the lines that are executable in PHP. So we declare the variable
$db and create a connection to the mysql database with the statement
“mysql_connect(“localhost”, “root”, “”)”. In plain English, it means connect to MySQL
database in localhost server with the username root and password “”. Replace them with
your own username and password if they are different.
Then we assign a pointer to this database to $db; in other words, $db points to our database
server localhost. Next we select the database with which we want to interact with the lines
“mysql_select_db(“learndb”,$db);” which means we wish to use the database “learndb”
located by the pointer variable $db. But we want information from the database, so we query
the database with the lines “$result = mysql_query(“SELECT * FROM personnel”,$db);” The
part “SELECT * FROM personnel” is an SQL statement (in case you don’t know SQL), which
means select all the stuff from the database personnel.

We run this query with the PHP command mysql_query() and save the result returned by the
database to the variable $result. Now we can access the different data in the different rows
of the database from the $result variable. We use the function mysql_fetch_array() to extract
each row from $result and assign them to variable $myrow. So $myrow contains information
about each row as opposed to all the rows in $result.
Then we output the data contained in each row. “echo $myrow[“firstname”];” means
send to output the value contained in the field “firstname” of the row contained in $myrow; in
other words, we access different fields of the row with $myrow[“fieldname”].
We have used the while() loop here, which means as long as or while there are data to be
extracted from $result, execute the lines within those brackets {}. Thus we get nicely
formatted output in our browser. Viewing the PHP code and the HTML source from the
browser side-by-side may help you easily understand the procedure. Congratulations! You
have created your first dynamic page.

Leave a Reply

Your email address will not be published. Required fields are marked *


*