Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 9682

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Overview</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="intro.mysqli.html">Introduction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.quickstart.html">Quick start guide</a></div>
 <div class="up"><a href="book.mysqli.html">Mysqli</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.overview" class="chapter">

  <h1>Overview</h1>


  <p class="para">
    This section provides an introduction to the options available to
    you when developing a PHP application that needs to interact with a
    MySQL database.
  </p>

  <p class="para">
    <em class="emphasis">What is an API?</em>
  </p>

  <p class="para">
    An Application Programming Interface, or API, defines the classes,
    methods, functions and variables that your application will need to
    call in order to carry out its desired task. In the case of PHP
    applications that need to communicate with databases the necessary
    APIs are usually exposed via PHP extensions.
  </p>

  <p class="para">
    APIs can be procedural or object-oriented. With a procedural API you
    call functions to carry out tasks, with the object-oriented API you
    instantiate classes and then call methods on the resulting objects.
    Of the two the latter is usually the preferred interface, as it is
    more modern and leads to better organized code.
  </p>

  <p class="para">
    When writing PHP applications that need to connect to the MySQL
    server there are several API options available. This document
    discusses what is available and how to select the best solution for
    your application.
  </p>

  <p class="para">
    <em class="emphasis">What is a Connector?</em>
  </p>

  <p class="para">
    In the MySQL documentation, the term <em class="emphasis">connector</em>
    refers to a piece of software that allows your application to
    connect to the MySQL database server. MySQL provides connectors for
    a variety of languages, including PHP.
  </p>

  <p class="para">
    If your PHP application needs to communicate with a database server
    you will need to write PHP code to perform such activities as
    connecting to the database server, querying the database and other
    database-related functions. Software is required to provide the API
    that your PHP application will use, and also handle the
    communication between your application and the database server,
    possibly using other intermediate libraries where necessary. This
    software is known generically as a connector, as it allows your
    application to <em class="emphasis">connect</em> to a database server.
  </p>

  <p class="para">
    <em class="emphasis">What is a Driver?</em>
  </p>

  <p class="para">
    A driver is a piece of software designed to communicate with a
    specific type of database server. The driver may also call a
    library, such as the MySQL Client Library or the MySQL Native
    Driver. These libraries implement the low-level protocol used to
    communicate with the MySQL database server.
  </p>

  <p class="para">
    By way of an example, the <a href="mysqli.overview.html#mysqli.overview.pdo" class="link">PHP
    Data Objects (PDO)</a> database abstraction layer may use one of
    several database-specific drivers. One of the drivers it has
    available is the PDO MYSQL driver, which allows it to interface with
    the MySQL server.
  </p>

  <p class="para">
    Sometimes people use the terms connector and driver interchangeably,
    this can be confusing. In the MySQL-related documentation the term 
    "<span class="quote">driver</span>" is reserved for software that provides
    the database-specific part of a connector package.
  </p>

  <p class="para">
    <em class="emphasis">What is an Extension?</em>
  </p>

  <p class="para">
    In the PHP documentation you will come across another term -
    <em class="emphasis">extension</em>. The PHP code consists of a core,
    with optional extensions to the core functionality. PHP&#039;s
    MySQL-related extensions, such as the <em>mysqli</em>
    extension, and the <em>mysql</em> extension, are
    implemented using the PHP extension framework.
  </p>

  <p class="para">
    An extension typically exposes an API to the PHP programmer, to
    allow its facilities to be used programmatically. However, some
    extensions which use the PHP extension framework do not expose an
    API to the PHP programmer.
  </p>

  <p class="para">
    The PDO MySQL driver extension, for example, does not expose an API
    to the PHP programmer, but provides an interface to the PDO layer
    above it.
  </p>

  <p class="para">
    The terms API and extension should not be taken to mean the same
    thing, as an extension may not necessarily expose an API to the
    programmer.
  </p>

  <p class="para">
    <em class="emphasis">What are the main PHP API offerings for using
    MySQL?</em>
  </p>

  <p class="para">
    There are three main API options when considering connecting to a
    MySQL database server:
  </p>

  <ul class="itemizedlist">

    <li class="listitem">
      <p class="para">
        PHP&#039;s MySQL Extension
      </p>
    </li>

    <li class="listitem">
      <p class="para">
        PHP&#039;s mysqli Extension
      </p>
    </li>

    <li class="listitem">
      <p class="para">
        PHP Data Objects (PDO)
      </p>
    </li>

  </ul>

  <p class="para">
    Each has its own advantages and disadvantages. The following
    discussion aims to give a brief introduction to the key aspects of
    each API.
  </p>

  <p class="para">
    <em class="emphasis">What is PHP&#039;s MySQL Extension?</em>
  </p>

  <p class="para">
    This is the original extension designed to allow you to develop PHP
    applications that interact with a MySQL database. The
    <em>mysql</em> extension provides a procedural interface
    and is intended for use only with MySQL versions older than 4.1.3.
    This extension can be used with versions of MySQL 4.1.3 or newer,
    but not all of the latest MySQL server features will be available.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      If you are using MySQL versions 4.1.3 or later it is
      <em class="emphasis">strongly</em> recommended that you use the
      <em>mysqli</em> extension instead.
    </p>
  </p></blockquote>

  <p class="para">
    The <em>mysql</em> extension source code is located in the
    PHP extension directory <var class="filename">ext/mysql</var>.
  </p>

  <p class="para">
    For further information on the <em>mysql</em> extension,
    see <a href="book.mysql.html" class="xref">Mysql</a>.
  </p>

  <p class="para">
    <em class="emphasis">What is PHP&#039;s mysqli Extension?</em>
  </p>

  <p class="para">
    The <em>mysqli</em> extension, or as it is sometimes
    known, the MySQL <em class="emphasis">improved</em> extension, was
    developed to take advantage of new features found in MySQL systems
    versions 4.1.3 and newer. The <em>mysqli</em> extension is
    included with PHP versions 5 and later.
  </p>

  <p class="para">
    The <em>mysqli</em> extension has a number of benefits,
    the key enhancements over the <em>mysql</em> extension
    being:

    <ul class="itemizedlist">

      <li class="listitem">
        <p class="para">
          Object-oriented interface
        </p>
      </li>

      <li class="listitem">
        <p class="para">
          Support for Prepared Statements
        </p>
      </li>

      <li class="listitem">
        <p class="para">
          Support for Multiple Statements
        </p>
      </li>

      <li class="listitem">
        <p class="para">
          Support for Transactions
        </p>
      </li>

      <li class="listitem">
        <p class="para">
          Enhanced debugging capabilities
        </p>
      </li>

      <li class="listitem">
        <p class="para">
          Embedded server support
        </p>
      </li>

    </ul>
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      If you are using MySQL versions 4.1.3 or later it is
      <em class="emphasis">strongly</em> recommended that you use this
      extension.
    </p>
  </p></blockquote>

  <p class="para">
    As well as the object-oriented interface the extension also provides
    a procedural interface.
  </p>

  <p class="para">
    The <em>mysqli</em> extension is built using the PHP
    extension framework, its source code is located in the directory
    <var class="filename">ext/mysqli</var>.
  </p>

  <p class="para">
    For further information on the <em>mysqli</em> extension,
    see <a href="book.mysqli.html" class="xref">Mysqli</a>.
  </p>

  <p class="para" id="mysqli.overview.pdo">
    <em class="emphasis">What is PDO?</em>
  </p>

  <p class="para">
    PHP Data Objects, or PDO, is a database abstraction layer
    specifically for PHP applications. PDO provides a consistent API for
    your PHP application regardless of the type of database server your
    application will connect to. In theory, if you are using the PDO
    API, you could switch the database server you used, from say
    Firebird to MySQL, and only need to make minor changes to your PHP
    code.
  </p>

  <p class="para">
    Other examples of database abstraction layers include JDBC for Java
    applications and DBI for Perl.
  </p>

  <p class="para">
    While PDO has its advantages, such as a clean, simple, portable API,
    its main disadvantage is that it doesn&#039;t allow you to use all of the
    advanced features that are available in the latest versions of MySQL
    server. For example, PDO does not allow you to use MySQL&#039;s support
    for Multiple Statements.
  </p>

  <p class="para">
    PDO is implemented using the PHP extension framework, its source
    code is located in the directory <var class="filename">ext/pdo</var>.
  </p>

  <p class="para">
    For further information on PDO, see the
    <a href="book.pdo.html" class="xref">PDO</a>.
  </p>

  <p class="para">
    <em class="emphasis">What is the PDO MYSQL driver?</em>
  </p>

  <p class="para">
    The PDO MYSQL driver is not an API as such, at least from the PHP
    programmer&#039;s perspective. In fact the PDO MYSQL driver sits in the
    layer below PDO itself and provides MySQL-specific functionality.
    The programmer still calls the PDO API, but PDO uses the PDO MYSQL
    driver to carry out communication with the MySQL server.
  </p>

  <p class="para">
    The PDO MYSQL driver is one of several available PDO drivers. Other
    PDO drivers available include those for the Firebird and PostgreSQL
    database servers.
  </p>

  <p class="para">
    The PDO MYSQL driver is implemented using the PHP extension
    framework. Its source code is located in the directory
    <var class="filename">ext/pdo_mysql</var>. It does not expose an API to the
    PHP programmer.
  </p>

  <p class="para">
    For further information on the PDO MYSQL driver, see
    <a href="ref.pdo-mysql.html" class="xref">MySQL (PDO)</a>.
  </p>

  <p class="para" id="mysqli.overview.mysqlnd">
    <em class="emphasis">What is PHP&#039;s MySQL Native Driver?</em>
  </p>

  <p class="para">
    In order to communicate with the MySQL database server the
    <em>mysql</em> extension, <em>mysqli</em> and
    the PDO MYSQL driver each use a low-level library that implements
    the required protocol. In the past, the only available library was
    the MySQL Client Library, otherwise known as
    <em>libmysqlclient</em>.
  </p>

  <p class="para">
    However, the interface presented by <em>libmysqlclient</em> was
    not optimized for communication with PHP applications, as
    <em>libmysqlclient</em> was originally designed with C
    applications in mind. For this reason the MySQL Native Driver,
    <em>mysqlnd</em>, was developed as an alternative to
    <em>libmysqlclient</em> for PHP applications.
  </p>

  <p class="para">
    The <em>mysql</em> extension, the
    <em>mysqli</em> extension and the PDO MySQL driver can
    each be individually configured to use either
    <em>libmysqlclient</em> or <em>mysqlnd</em>. As
    <em>mysqlnd</em> is designed specifically to be utilised
    in the PHP system it has numerous memory and speed enhancements over
    <em>libmysqlclient</em>. You are strongly encouraged to take
    advantage of these improvements.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      The MySQL Native Driver can only be used with MySQL server
      versions 4.1.3 and later.
    </p>
  </p></blockquote>

  <p class="para">
    The MySQL Native Driver is implemented using the PHP extension
    framework. The source code is located in
    <var class="filename">ext/mysqlnd</var>. It does not expose an API to the
    PHP programmer.
  </p>



  <p class="para">
    <em class="emphasis">Comparison of Features</em>
  </p>

  <p class="para">
    The following table compares the functionality of the three main
    methods of connecting to MySQL from PHP:
  </p>

  <table class="doctable table">
   <caption><strong>Comparison of MySQL API options for PHP</strong></caption>
    
      <col width="25*" />
      <col width="25*" />
      <col width="25*" />
      <col width="25*" />
      <thead>
        <tr>
          <th class="empty">&nbsp;</th>
          <th>PHP&#039;s mysqli Extension</th>
          <th>PDO (Using PDO MySQL Driver and MySQL Native Driver)</th>
          <th>PHP&#039;s MySQL Extension</th>
        </tr>

      </thead>

      <tbody class="tbody">
        <tr>
          <td>PHP version introduced</td>
          <td>5.0</td>
          <td>5.0</td>
          <td>Prior to 3.0</td>
        </tr>

        <tr>
          <td>Included with PHP 5.x</td>
          <td>yes</td>
          <td>yes</td>
          <td>Yes</td>
        </tr>

        <tr>
          <td>MySQL development status</td>
          <td>Active development</td>
          <td>Active development as of PHP 5.3</td>
          <td>Maintenance only</td>
        </tr>

        <tr>
          <td>Recommended by MySQL for new projects</td>
          <td>Yes - preferred option</td>
          <td>Yes</td>
          <td>No</td>
        </tr>

        <tr>
          <td>API supports Charsets</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>No</td>
        </tr>

        <tr>
          <td>API supports server-side Prepared Statements</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>No</td>
        </tr>

        <tr>
          <td>API supports client-side Prepared Statements</td>
          <td>No</td>
          <td>Yes</td>
          <td>No</td>
        </tr>

        <tr>
          <td>API supports Stored Procedures</td>
          <td>Yes</td>
          <td>Yes</td>
          <td>No</td>
        </tr>

        <tr>
          <td>API supports Multiple Statements</td>
          <td>Yes</td>
          <td>Most</td>
          <td>No</td>
        </tr>

        <tr>
          <td>Supports all MySQL 4.1+ functionality</td>
          <td>Yes</td>
          <td>Most</td>
          <td>No</td>
        </tr>

      </tbody>
    
  </table>


</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="intro.mysqli.html">Introduction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.quickstart.html">Quick start guide</a></div>
 <div class="up"><a href="book.mysqli.html">Mysqli</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>