MySQL
  Home arrow MySQL arrow Page 3 - Working with PHP and MySQL
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? 
Google.com  
MYSQL

Working with PHP and MySQL
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2007-05-24


    Table of Contents:
  • Working with PHP and MySQL
  • Fetching and Displaying
  • Putting It All Together
  • Using PEAR

  • 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


    Working with PHP and MySQL - Putting It All Together
    ( Page 3 of 4 )

    Now you're going to take all of the steps and put them into a single PHP file that you'll call db_test.php. You should place the PHP script shown in Example 9-5 in the same directory as the db_login.php file.

    Example 9-5.  Displaying the books and authors

    <?php
    // Include our login information
    include('db_login.php');
    // Connect
    $connection = mysql_connect( $db_host, $db_username, $db_password );
    if (!$connection)
    {
      
    die ("Could not connect to the database:
    <br />". mysql_error());
    }
    // Select the database $db_select=mysql_select_db($db_database); if (!$db_select)
    {
      
    die ("Could not select the database:
    <br />". mysql_error());
    }
    // Assign the query
    $query = "SELECT * FROM `books` NATURAL JOIN `authors`";
    // Execute the query
    $result = mysql_query( $query );
    if (!$result)
    {
      
    die ("Could not query the database:
    <br />". mysql_error());
    }

    // Fetch and display the results
    while ($result_row = mysql_fetch_row(($result)))
    {

           echo 'Title: '.$result_row[1] . '
    <br />';
           echo 'Author: '.$result_row[4] . '
    <br />';
           echo 'Pages: '.$result_row[2] . '
    <br />';
    }
    / /Close the connection
    mysql_close($connection);
    ?>

    Here's the output from Example 9-5:

      Title: Linux in a Nutshell<br />Author: Ellen Siever<br /> Pages: 476<br />
      <br />Title: Linux in a Nutshell<br />
    Author: Aaron Weber<br /> Pages: 476<br />
      <br />Title: Classic Shell Scripting<br> Author: Arnold Robbins<br /> Pages: 256<br />
      <br />Title: Classic ShellScripting<br />
    Author: Nelson H.F. Beebe<br /> Pages:
    256<br /><br />


    This displays in your browser as in Figure 9-3.

    If you don't see the screen in Figure 9-3, then you'll see an error from whichever step in the process had a problem, giving you an idea of what went wrong and where it was wrong.

    To make the display more appealing, you can put the information into a table, as shown in Example 9-6. You also add complete HTML headers.

     

    Figure 9-3.  How Example 9-5 displays in the browser

    Example 9-6.  Displaying the results of a query in an HTML table

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Displaying in an HTML table</title> </head>
    <body>
    <table border="1">
    <tr>
    <th>Title</th>
    <th>Author</th>
    <th>Pages</th>
    </tr>
    <?php
    //Include our login information
    include('db_login.php');
    // Connect
    $connection = mysql_connect($db_host, $db_username, $db_password);
    if (!$connection){
    die("Could not connect to the database:
    <br />". mysql_error());
    }
    // Select the database
    $db_select = mysql_select_db($db_database); if (!$db_select){
    die ("Could not select the database:
    <br />". mysql_error());
    }
    // Assign the query
    $query = "SELECT * FROM `books` NATURAL JOIN `authors`";
    // Execute the query
    $result = mysql_query($query);
    if (!$result){
    die ("Could not query the database:
    <br />". mysql_error());
    }
    // Fetch and display the results
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $title = $row["title"];
    $author = $row["author"];
    $pages = $row["pages"];
    echo "<tr>";
    echo "<td>$title</td>";
    echo "<td>$author</td>";
    echo "<td>$pages</td>";
    echo "</tr>";
    }
    // Close the connection
    mysql_close($connection);
    ?>
    </table>
    </body>
    </html>
     

    Example 9-6.  displays in your browser as shown in Figure 9-4.


    Figure 9-4.  The same data but in an HTML table

    Notice that you made use of the MYSQL_ASSOC fetch type in Example 9-6. You're probably saying to yourself, "That's great, but how do I display the book titles with the authors all on one line?" This is where we talk about PEAR.



     
     
    >>> More MySQL Articles          >>> More By O'Reilly Media
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek