PHP
  Home arrow PHP arrow Page 5 - PHP 101 (part 4) - Look, Ma...It's Ali...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 3 stars3 stars3 stars3 stars3 stars / 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:
      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


    PHP 101 (part 4) - Look, Ma...It's Alive! - ...For Different Folks


    (Page 5 of 10 )

    You can use PHP's mysql_fetch_row() function to obtain a simple array of values, and then use these values according to the array index - a slightly different variation of the technique used above. Take a look:

    <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 // and return an array named $myrow while ($myrow = mysql_fetch_row($result)) { echo "<tr><td width=150 align=left>$myrow[0]</td><td width=100 align=left><a href=$myrow[2]>$myrow[1]</a></td><td width=100 align=left><a href=$myrow[4] >$myrow[3]</a></td><td width=100 align=left><a href=$myrow[6]> $myrow[5]</a></td></tr>"; } // memory flush mysql_free_result($result); ?> </body> </html>
    You can also use PHP's mysql_fetch_row() and list() functions to obtain a simple array of values, and then assign these values to different variables - another variation of the technique we've shown you above. Take a look - only the "while" loop will change:

    <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 // and the values returned will be assigned to different // variables via list() while (list($user, $title1, $url1, $title2, $url2, $title3, $url3) = mysql_fetch_row($result)) { 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>
    In this case, the list() function is used to assign different elements of the result set t o PHP variables, which are then used when rendering the page.

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


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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