MySQL
  Home arrow MySQL arrow Page 3 - Implementing Additional Methods with mysqli and PHP 5
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

Implementing Additional Methods with mysqli and PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2006-07-10


    Table of Contents:
  • Implementing Additional Methods with mysqli and PHP 5
  • Fetching rows, finding IDs and moving result set pointers: implementing the “fetch_array()” and “data_seek()” methods
  • Counting fields and retrieving rows in a faster way: using the “fetch_assoc()” method and the “field_count” property
  • Getting information about table fields: using the “fetch_field()”, field_seek()” methods and the “current_field” property

  • 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


    Implementing Additional Methods with mysqli and PHP 5 - Counting fields and retrieving rows in a faster way: using the “fetch_assoc()” method and the “field_count” property
    ( Page 3 of 4 )

    In the previous section, I explained how to use the “fetch_array()” method, in order to fetch rows from a specific result set by using the “MYSQLI_ASSOC” and “MYSQLI_NUM” constants. However, as you may have guessed, the “mysqli” extension also exposes the “fetch_assoc()” method. This method behaves nearly identical to the popular “mysql_fetch_assoc()” function, and can be used for retrieving rows from a result set directly as an associative array. The example below demonstrates how to use the method in question:

    // example of 'fetch_assoc()' method
    $mysqli=new mysqli('host','user','password','database');
    if(mysqli_connect_errno()){
        trigger_error('Error connecting to host. '.$mysqli-
    >error,E_USER_ERROR);
    }
    $sql="SELECT * FROM customers";
    if($result=$mysqli->query($sql)){
       // fetch associative array
       while($row=$result->fetch_assoc()){
           echo 'ID: '.$row['id'].' Name: '.$row['name'].' Email:
    '.$row['email'].'<br />';
       }
       // close result set
       $result->close();
    }
    // close connection
    $mysqli->close();

    After executing the above script, the corresponding output will be the following:

    ID:1 Name: customer1 Email: email1@domain.com
    ID:2 Name: customer2 Email: email2@domain.com
    ID:3 Name: customer3 Email: email3@domain.com

    All right, I must admit that the above example is pretty simple, thus let me teach you how to use some helpful methods and properties that return information about fields of database tables.

    To begin with, there’s the “field_count” property, which can be used to determine the number of fields returned by a result set. Here’s an example that shows how to use this property; have a look at the source code:

    // example of 'field_count' property
    $mysqli=new mysqli('host','user','password','database');
    if(mysqli_connect_errno()){
        trigger_error('Error connecting to host. '.$mysqli-
    >error,E_USER_ERROR);
    }
    if($result=$mysqli->query("SELECT * FROM customers")){
        // count number of fields
        echo 'Number of fields in result set: '.$result->field_count;
        $result->close();
    }
    // close connection
    $mysqli->close();

    The example shown above illustrates how to determine the number of fields returned by a result set by using the pertinent “field_count” property that you saw before. As you’ll realize, using this property is a no-brainer process, but at the same time it opens up the door for exploring other methods and properties focused specifically on getting information about table fields.

    In the next few lines, I’ll pay strong attention to some of these methods, therefore click on the link below and continue reading.



     
     
    >>> More MySQL Articles          >>> More By Alejandro Gervasio
     

       

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek