Database Articles
  Home arrow Database Articles arrow Page 8 - Create dynamic sites with PHP & MySQL
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

Create dynamic sites with PHP & MySQL
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 25
    2002-05-19

    Table of Contents:
  • Create dynamic sites with PHP & MySQL
  • Introduction and installation
  • Installing Apache server routines
  • Installing MySQL
  • Installing PHP
  • Your first script
  • Your first database
  • Where's my view?
  • Creating an HTML form
  • Putting it together
  • Passing variables
  • Viewing individual rows
  • Deleting rows
  • Editing data
  • Searching our data
  • Tips for common tasks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Database Articles Articles
    More By Codewalkers


       · Hi :-)Thank you for this great tutorial!Best regardsU.
       · Very easy to use and follow. There are a few errors with the examples but i found...
       · Well organized, super-clear tutorial.Thanks so much!
       · thanks u for this tutorial it's really user friendly and helpful!=)luvvies,cat
       · I am so glad that I found this tutorial. It has a very comfortable rate of progress...
       · This tutorial sets out to do great things. And when you fix all the code, it really...
       · Hey, you have a few spaces where there not needed, so anyone who gets the...
       · usando esse tutorial você aprende muitas coisas boas sobre mysql e php...
       · 'nuff said
       · its good, but could be made more better by putting more examples
       · I am an idiot among the newbies.Many other tutorials never mention the $_POST[]...
       · Thanks!
       · Congratulations for your hard work to help people like me.This was very important...
       · This helped me get started with mySql and PHP. Thanks a lot!
       · This one of the greatest and most comprehensive tutorials i have read in a long...
       · This one of the greatest and most comprehensive tutorials i have read in a long...
       · 
       · This tutorial helped me make my site for 30 % and 35% exists from databases (= thank...
       · This very useful for me as a new comer. The way tutorial is presented is very...
       · IM KNEELING DOWN IN AWE...MASTER!!! MASTER!!!
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT