PHP
  Home arrow PHP arrow Page 3 - 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! - Hello Database!
    ( Page 3 of 10 )

    All working? Good. Now, let's use PHP to do exactly the same thing - fire a SELECT query at the database, and display the results in an HTML page.


    <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); // formulate the SQL query - same as above $query = "select count(*) from url_list"; // run the query on the database // assume the database is named "php101" $result = mysql_db_query($db,$query ,$connection); // assign the result to a variable $counter = mysql_result($result,0); // and display it echo "There are $counter records in the database"; // free up used memory mysql_free_result($result); ?> </body> </html>
    And you'll see something like this:

    There are 2 records in the database
    As you can see, using PHP to get data from a database involves several steps, each of which is actually a pre-defined PHP function. Let's dissect each step:

    1. The first thing to do is specify some important information needed to establish a connection to the database server. This information includes the server name, the username and password required to gain access to it, and the name of the database to query. These values are all set up in regular PHP variables.

    2. In order to begin communication with the mySQL database server, you first need to open a connection to the server. All communication between PHP and the database server takes place through this connection.

    In order to initialize this connection, PHP offers the mysql_connect() function.

    $connection = mysql_connect($server, $user, $pass);
    The function requires three parameters: the name of the server, and the mySQL username and password. If the database server and the Web server are both running on the same physical machine, the server name is usually "localhost"

    This function then returns a "link identifier", which is stored in the variable $connection; this identifier is used throughout the script when communicating with the database.

    3. Now that you have a connection to the database, it's time to send it a query via the mysql_db_query() function. This function also needs three parameters: the database name, the query string and the link identifier for the connection.

    $query = "select count(*) from url_list"; $result = mysql_db_query($db, $query, $connection);
    The result set returned by the function above is stored in the variable $result. This result set may contain, depending on your query, one or more rows or columns of data. You then need to retrieve specific sections or subsets of the result set with different PHP functions - the one we've used is the mysql_result() function, which uses the result variable and the row number to return the value you need. You can optionally add the column name as well to get to a specific value.

    $counter = mysql_result($result,0);
    There are several other efficient alternatives to this function, which will be explained a little further down.

    4. Finally, each result set returned after a query occupies some amount of memory - and if your system is likely to experience heavy load, it's a good idea to use the mysql_free_result() function to free up the used memory.

     
     
    >>> 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