Create dynamic sites with PHP & MySQL - Where's my view?
(Page 8 of 16 )
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.
Next: Creating an HTML form >>
More Database Articles Articles
More By Codewalkers
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|