MySQL
  Home arrow MySQL arrow Page 3 - The Three Most Important MySQL Queries
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

The Three Most Important MySQL Queries
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2009-07-06


    Table of Contents:
  • The Three Most Important MySQL Queries
  • The INSERT MySQL Query Command
  • The SELECT MySQL Query Command
  • The UPDATE MySQL Query Command

  • 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


    The Three Most Important MySQL Queries - The SELECT MySQL Query Command
    ( Page 3 of 4 )

    So we now have data in our database. If we would like to grab data from it, we will need the SELECT command. There are several options for grabbing data.

    Option 1: Grabbing the entire data table out of the database:

    SELECT * FROM `tablename`

    Here is the PHP script to illustrate this example. It grabs the entire MySQL data table and displays it in your browser.

    <?php

    //Step 1 Connect to database

    $username = "Your MySQL username here";

    $password = "Your MySQL password";

    $hostname = "Hostname";

    $table = "MySQL Table name where the data will be inserted";

    $database = "The name of the MySQL database which holds the table";

    $dbhandle = mysql_connect($hostname, $username, $password)

    or die("Unable to connect to MySQL");

    $selected = mysql_select_db($database,$dbhandle)

    or die("Could not select $database");

    //Step 2. Insert other PHP scripts here (such as grabbing data from HTML forms, etc)

    //Step 3. Sanitize variables before inserting to database. This will prevent MySQL injection.

    $categories_id = mysql_real_escape_string(stripslashes($categories_id));

    $language_id = mysql_real_escape_string(stripslashes($language_id));

    $categories_name = mysql_real_escape_string(stripslashes($categories_name));

    $result = mysql_query("SELECT * FROM `categories_description`")

    or die(mysql_error());

    echo '<table border="1">';

    echo '<tr>';

    echo '<td><b>Categories ID</b></td>';

    echo '<td><b>Language ID</b></td>';

    echo '<td><b>Categories Name</b></td>';

    echo '</tr>';

    while($row = mysql_fetch_array($result)){

    echo '<tr>';

    echo '<td>'.$row['categories_id'].'</td>';

    echo '<td>'.$row['language_id'].'</td>';

    echo '<td>'.$row['categories_name'].'</td>';

    echo '</tr>';

    }

    echo '</table>';

    ?>

    This will display the table in the HTML browser, which looks like this:

    Option 2: Extracting specific data from a specific fieldname in the MySQL table.

    There are cases when you need to extract specific data from a specific fieldname that satisfies certain conditions.

    An example of this application is when you need to extract the customer email address if they can correctly provide the username and password. So there could be three fields in the table: username, password, and emailaddress.

    So, to extract the customer email address in a MySQL table, for example customertable, the PHP query could be:

    <?php

    //Connect to database...

    //Sanitize the variables..

    //MySQL query

    $result = mysql_query("SELECT `emailaddress` FROM `customertable` WHERE `username`='$username' AND `password`='$password'")

    or die(mysql_error());

    $row = mysql_fetch_array($result)

    or die("Invalid query: " . mysql_error());

    $emailaddress = $row['emailaddress'];

    echo "Your email address is: $emailaddress";

    ?>

    In this PHP MySQL query, the WHERE statement specifies the condition under which it pulls out an email address matching a certain username AND a password.

    You can query MySQL using several AND statements, as long as it is correctly done.



     
     
    >>> More MySQL Articles          >>> More By Codex-M
     

       

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