PHP
  Home arrow PHP arrow Page 2 - Databases and PHP
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? 
PHP

Databases and PHP
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-06-14


    Table of Contents:
  • Databases and PHP
  • Relational Databases and SQL
  • PEAR DB Basics
  • Data Source Names

  • 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


    Databases and PHP - Relational Databases and SQL
    ( Page 2 of 4 )

    A Relational Database Management System (RDBMS) is a server that manages data for you. The data is structured into tables, where each table has a number of columns, each of which has a name and a type. For example, to keep track of science fiction books, we might have a “books” table that records the title (a string), year of release (a number), and the author.

    Tables are grouped together into databases, so a science fiction book database might have tables for time periods, authors, and villains. An RDBMS usually has its own user system, which controls access rights for databases (e.g., “user Fred can update database authors”).

    PHP communicates with relational databases such as MySQL and Oracle using the Structured Query Language (SQL). You can use SQL to create, modify, and query relational databases.

    The syntax for SQL is divided into two parts. The first, Data Manipulation Language, or DML, is used to retrieve and modify data in an existing database. DML is remarkably compact, consisting of only four verbs: SELECT , INSERT , UPDATE , and DELETE . The set of SQL commands used to create and modify the database structures that hold the data is known as Data Definition Language, or DDL. The syntax for DDL is not as standardized as that for DML, but as PHP just sends any SQL commands you give it to the database, you can use any SQL commands your database supports.

    The SQL Command file for creating this sample library database is available in a file called library.sql.

    Assuming you have a table called books , this SQL statement would insert a new row (check web site companion for source files):

      INSERT INTO books VALUES (4, 'I, Robot', '0-553-29438-5', 1950)

    This SQL statement inserts a new row but specifies the columns for which there are values:

      INSERT INTO books (authorid, title, ISBN, pub_year) VALUES (4, 'I, Robot, '0-553-29438-5', 1950)

    To delete all books that were published in 1979 (if any), we could use this SQL statement:

      DELETE FROM books WHERE pub_date = 1979

    To change the year for Roots to 1983, use this SQL statement:

      UPDATE books SET pub_year=1983 WHERE title='Roots'

    To fetch only the books published in the 1980s, use:

      SELECT * FROM books WHERE pub_year > 1979 AND pub_year < 1990

    You can also specify the fields you want returned. For example:

      SELECT title, pub_year FROM books WHERE pub_year > 19790 AND pub_year < 1990

    You can issue queries that bring together information from multiple tables. For example, this query joins together the book and author tables to let us see who wrote each book:

      SELECT authors.name, books.title FROM books, authors
     
    WHERE authors.authorid = books.authorid.

    For more on SQL, see SQL in a Nutshell, by Kevin Kline (O’Reilly).



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT