Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2006
    Posts
    1

    Exclamation Unanswered: Which one is better?

    Hi all
    I wanna know, which one of the following code is better (don't give a damn about the coding language, I just wanna know about SQL query):

    Code1:

    PHP Code:
    $query "SELECT * FROM products, categories, options WHERE products.product_id = '".$_REQUEST["id"]."' AND categories.prod_id = products.product_id AND options.category_id = categories.category_id";
    $result mysql_query($query);

    for (
    $i=0$i<mysql_num_rows($result); $i++)
    {
         
    $record mysql_fetch_array($result);
         
    // Showing results ...

    Code 2:

    PHP Code:
    $query "SELECT * FROM products WHERE products.product_id = '".$_REQUEST["id"]."'";
    $result mysql_query($query);

    $record mysql_fetch_array($result);
    // Showing some results ...

    $query "SELECT * FROM categories WHERE categories.product_id = '".$_REQUEST["id"]."'";
    $result mysql_query($query);
    for (
    $i=0$i<mysql_num_rows($result); $i++)
    {
         
    $record mysql_fetch_array($result);
         
    $category $record["category_id"]; 

         
    $query "SELECT * FROM options WHERE options.category_id = '$category'";
         
    $result2 mysql_query($query);
         for (
    $j=0$j<mysql_num_rows($result2); $j++)
         {
              
    $row mysql_fetch_array($result2);
              
    // Show results ...
         
    }

    This is just a sample, of course I must have similar code for 5 tables (here is only 3 tables). So the first code will contain a more complicated query. But the second one contains more query. Which one is faster, and has better coding?

    Maybe useful, products has some fields, including product_id. categories includes both product_id and category_id, and options contains option_id and category_id.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by rushenas
    Which one is faster
    the first one


    Quote Originally Posted by rushenas
    and has better coding?
    the first one
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •