MySQL
  Home arrow MySQL arrow Page 4 - 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 - Preparing SQL queries: using the “prepare()”, bind_param()” and “execute()” methods
    ( Page 4 of 4 )

    As I said before, you can prepare a query before it is executed, and insert markers inside the query in question, which will be replaced with real values. Again, this process will be best understood with an example, thus take a look at the code listing below, which uses a set of new methods to first prepare a query and then execute it:

    $mysqli=new mysqli('host','user','password','database');
    if(mysqli_connect_errno()){
        trigger_error('Error connecting to host. '.$mysqli-
    >error,E_USER_ERROR);
    }
    // define parameter
    $name='Alejandro Gervasio';
    // prepare query
    if($stat=$mysqli->prepare("SELECT email FROM users WHERE
    name=?")){
        // bind parameter to marker
        $stat->bind_param('s',$name);
        // run query
        $stat->execute();
        // bind result set to $email variable
        $stat->bind_result($email);
        // fetch result
        $stat->fetch();
        // display row
        echo 'User with name '.$name.' has the following email
    address: '.$email;
    }
    // close connection
    $mysqli->close();

    Examine the snippet shown above. As you can see, it uses some new methods, which work together to prepare a SELECT statement and then run it.

    In this example, after defining the $name parameter, the “prepare()” method prepares the query, which includes only one marker defined by a question sign (?).  Then, the parameter is bound to this marker as a string value (hence the “s” argument) via the “bind_param()” method, and finally the query is run by the “execute()” method.

    So far, this process is fairly comprehensive, but how does the script fetch the corresponding result from the database? Here is where the “bind_result()” and “fetch()” methods take place, since the first one attaches the corresponding result to the $email variable, while the second one populates this variable with the values of the returned row.

    Assuming that I included myself as a new row of the “USERS” database table, the previous example would return the following result:

    User with name Alejandro Gervasio has the following email
    address: alejandro@mydomain.com

    Wasn’t that cool? I’m sure you’ll agree with me that preparing queries with the appropriate markers is a very powerful feature of the “mysqli” extension. Try including additional markers within your queries and see what happens in each case. It’s really worthwhile, trust me.

    To wrap up

    That’s all for the moment. Over this first article, I covered some of the most significant methods and properties that come with the “mysqli” library. From connecting to MySQL and running multiple queries, to binding SQL statements to specific parameters, the experience hopefully has been instructive.

    In the second tutorial, I’ll be demonstrating how to use the useful “commit()” and “rollback()” methods, among other handy things, so you don’t have excuses to miss it! 



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