MySQL
  Home arrow MySQL arrow Page 3 - Performing Basic Tasks with MySQL 4.1 and Above, using mysqli with 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

Performing Basic Tasks with MySQL 4.1 and Above, using mysqli with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 16
    2006-06-27


    Table of Contents:
  • Performing Basic Tasks with MySQL 4.1 and Above, using mysqli with PHP 5
  • Performing basic operations: connecting to MySQL, running queries and more
  • Leveraging the real power of the “mysqli” extension: running multiple queries
  • Preparing SQL queries: using the “prepare()”, bind_param()” and “execute()” methods

  • 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


    Performing Basic Tasks with MySQL 4.1 and Above, using mysqli with PHP 5 - Leveraging the real power of the “mysqli” extension: running multiple queries
    ( Page 3 of 4 )

    If you’re anything like me, then I’m sure that very often your PHP applications must run numerous queries. The good news is that the “mysqli” library offers some helpful methods for running multiple queries and dealing with more than one result set.

    With reference to this improved functionality, the following example illustrates the process for executing two SELECT statements in one single pass, by using some new methods such as “multi_query()”, “store_result()”, more_result()” and “next_result()” respectively. Take a look at the source code:

    $mysqli=new mysqli('host','user','password','database');
    if(mysqli_connect_errno()){
        trigger_error('Error connecting to host. '.$mysqli-
    >error,E_USER_ERROR);
    }
    // run multiple queries
    $sql="SELECT * FROM users;SELECT name FROM USERS";
    if($mysqli->multi_query($sql)){
        do{
            // store first result set
            if($result=$mysqli->store_result()){
                while($row=$result->fetch_array(MYSQLI_ASSOC)){
                    echo $row['id'].' '.$row['name'].' '.$row
    ['email'].'<br />';
                }
                $result->close();
            }
            // display header of second result set
            if($mysqli->more_results()){
                echo 'Displaying results of second query...<br />';
            }
       }while($mysqli->next_result());
    }
    // close connection
    $mysqli->close();

    In this case, I first concatenated the two SELECT statements with a semicolon (;) character, and then executed them by using the “multi_query()” method. Since this process also returns two result sets, I utilized a “do-while” loop, in order to display the first dataset, and next show the second one.

    As you can see, the script’s driving logic is implemented around the “next_result()” method, which prepares the subsequent result set after calling the “multi_query()” method. In short, both data sets are processed in the following sequence: the first one is transferred to the $result object by the “store_result()” method and then displayed properly, while the second one is shown after checking for an additional result set by utilizing the “more_result()” method.

    Well, all these tasks probably sound a little confusing in theory, so allow me to show you a practical case. Say you have the following rows stored in a “USERS” table:

    1 User 1  user1@domain.com
    2 User 2  user2@domain.com
    3 User 3  user3@domain.com
    4 User 4  user4@domain.com
    5 User 5  user5@domain.com

    Based on the script listed above, the corresponding output would be the following:

    1 User 1  user1@domain.com
    2 User 2  user2@domain.com
    3 User 3  user3@domain.com
    4 User 4  user4@domain.com
    5 User 5  user5@domain.com

    Displaying results of second query...

    User 1
    User 2
    User 3
    User 4
    User 5

    As you can see, the “mysqli” extension will let you run multiple queries in a fairly accessible way, as well as process multiple result sets, something that was clearly demonstrated by the example you saw before. The set of previous methods implemented together shows a comprehensive approach for running several SQL statements in one single shot, and eventually manipulating all the returned result sets.

    So far, you learned how to run multiple queries, in addition to handling more than one MySQL data set at the same time. Quite probably, you’re thinking, "is that all there is to the 'mysqli' extension?" Of course not, since there are many more features that I'd like to show you.

    Therefore, over the next few lines, I’ll be explaining how to prepare queries by using the pertinent “prepare()” method, which can be used to insert parameters inside your SQL statements. To see how this will be done, please keep 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek