PHP
  Home arrow PHP arrow Page 6 - PHP 101 (part 4) - Look, Ma...It's Alive!
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PHP

PHP 101 (part 4) - Look, Ma...It's Alive!
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2000-09-01


    Table of Contents:
  • PHP 101 (part 4) - Look, Ma...It's Alive!
  • Dumped!
  • Hello Database!
  • Different Strokes...
  • ...For Different Folks
  • What's In A Name?
  • New Friends
  • Today's Special
  • Nuking The People
  • Oops!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    PHP 101 (part 4) - Look, Ma...It's Alive! - What's In A Name?
    ( Page 6 of 10 )

    Now that you've got the basics straight, let's evolve the application a little further. Our next example will ask the user to enter a user name. If it's a valid user name, the script will connect to the database and display the corresponding list of URLs; if not, it'll return an error.

    We'll be using a single page for the entire operation - the $submit variable [you remember this technique, don't you?] is used to decide whether to display the initial form or the result page. Take a look:

    <?php // check if the form has been submitted if($submit) { // initialize database connection $conn = mysql_connect("localhost", "test", "test"); mysql_select_db("php101", $conn); $result = mysql_query("select title1,url1,title2,url2,title3,url3 from url_list where uid = '$username'", $conn); // check if the user name is valid $num_rows = mysql_num_rows($result); // if no rows are returned then the username is invalid if(!$num_rows) { ?> <html> <head> <basefont face="Arial"> </head> <body> <center> <font size="3">User name not found!</font><br> <a href="list.php4">Click here to try again.</a> </center> </body> </html> <?php } else { // else if user name is valid // display list $result = mysql_query("select title1,url1,title2,url2,title3,url3 from url_list where uid = '$username'", $conn); list($title1,$url1,$title2,$url2,$title3,$url3) = mysql_fetch_row($result); ?> <html> <head> <basefont face="Arial"> </head> <body> <center> <font size="3">Welcome, <?php echo $username; ?>!</font><br> Pick your destination: <p> <?php echo "<a href=$url1>$title1</a>"; ?> <?php echo "<a href=$url2>$title2</a>"; ?> <?php echo "<a href=$url3>$title3</a>"; ?> </center> </body> </html> <?php mysql_free_result($result); } } else { // $submit not found // so display a form ?> <html> <head> <basefont face="Arial"> </head> <body> <form action="list.php4" method="POST"> <table> <tr> <td> Username: </td> <td> <input type="text" name="username" length=10 maxlength="30"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="Log in"> </td> </tr> </table> </form> </body> </html> <?php } ?>
    Log in as "bill" or "john" and watch as PHP connects to the database and retrieves that user's list of sites.

    If you look closely, you'll see that the script above actually contains three HTML pages embedded within it, one for each of the three possible scenarios. The first time the user visits the page, the variable $submit will not be set, and so a simple HTML form is displayed; this forms asks for a user name, which is stored in the variable $username.

    Once the form has been submitted, the same page is called again; however, this time around, the $submit variable will be found and so PHP will initiate a connection to the database to check if the user name is valid.

    One simple technique that can be used for simple "yes/no" type queries [such as the one above] is the mysql_num_rows() function; this function tells you the number of result rows returned by the query. A value of zero implies that no rows were found, which in turn implies that the user was not found.

    The result of mysql_num_rows() is stored in the variable $num_rows. If this variable exists, it implies that the user name is valid, and it's then possible to move ahead and fetch the Web addresses corresponding to that user name from the database. These addresses are then formatted and displayed on the page as active hyperlinks.

     
     
    >>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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