PHP
  Home arrow PHP arrow Page 4 - 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! - Different Strokes...
    ( Page 4 of 10 )

    What you just saw was a very basic example. Our next example will query the database, return the list of users with their URL list, and display it all in a neat


    <html> <head> </head> <body> <?php // set up some variables // server name $server = "localhost"; // username $user = "test"; // password $pass = "test"; // database to query $db = "php101"; // open a connection to the database $connection = mysql_connect($server, $user, $pass) or die("Invalid server or user"); // formulate the SQL query $query = "select * from url_list" or die("Error in query"); // run the query on the database $result = mysql_db_query($db, $query, $connection) or die("Error in query"); // display the result echo "<table width=450 border=1 cellspacing=0 cellpadding=0>"; echo "<tr><td width=150 align=left>User</td><td width=100 align=left>Site #1</td><td width=100 align=left>Site #2</td><td width=100 align=left>Site #3</td></tr>"; // with a while loop // this loop will iterate as many times as there are records while($myrow = mysql_fetch_array($result)) { $user = $myrow["uid"]; $title1 = $myrow["title1"]; $url1 = $myrow["url1"]; $title2 = $myrow["title2"]; $url2 = $myrow["url2"]; $title3 = $myrow["title3"]; $url3 = $myrow["url3"]; echo "<tr><td width=150 align=left>$user</td><td width=100 align=left><a href=$url1>$title1</a></td><td width=100 align=left><a href=$url2>$title2</a></td><td width=100 align=left><a href=$url3>$title3</a></td></tr>"; } // memory flush mysql_free_result($result); ?> </body> </html>
    As in our previous example, the script first sets up a connection to the database. The query is formulated and the result set is returned to the browser. In this case, since there's going to be much more data than before, it's a good idea to use the mysql_fetch_array() function - this function returns the values from the database as an enumerated array, allowing you to access each element by the column name

    $user = $myrow["uid"]; $title1 = $myrow["title1"]; $url1 = $myrow["url1"];
    and so on. Note our use of a "while" loop to parse and display each available record of the result set, and of the die() function to exit the script with an error message should there be an error in the configuration information or the query string. And here's the output:

    <html> <head> </head> <body> <table width=450 border=1 cellspacing=0 cellpadding=0> <tr><td width=150 align=left>User</td><td width=100 align=left>Site #1</td><td width=100 align=left>Site #2</td><td width=100 align=left>Site #3</td></tr> <tr><td width=150 align=left>john</td><td width=100 align=left><a href=http://www.melonfire.com>Melonfire</a></td><td width=100 align=left><a href=http://www.devshed.com>Devshed</a></td><td width=100 align=left><a href=http://www.php.net>PHP.Net</a></td></tr> <tr><td width=150 align=left>bill</td><td width=100 align=left><a href=http://www.yahoo.com>Yahoo</a></td><td width=100 align=left><a href=http://www.slashdot.org>Slashdot</a></td><td width=100 align=left><a href=http://www.32bit.com>32Bit.com</a></td></tr> </table> </body> </html>
    Discuss PHP 101 (part 4) - Look, Ma...It's Alive!
     
    Trying to get an overview, but when I try to go to part 3 I get taken to Part three...
    >>> Post your comment now!
     


     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT