www.webdeveloper.com
Results 1 to 8 of 8

Thread: More effecient coding...

  1. #1
    Join Date
    Nov 2006
    Posts
    250

    More effecient coding...

    Is there a more effecient way to do this??

    PHP Code:
    // Retrieve all the data from the "example" table
    $result1 mysql_query("SELECT * FROM home WHERE id='1'");
    $result2 mysql_query("SELECT * FROM home WHERE id='2'");

    // store the record of the "example" table into $row
    $row1 mysql_fetch_array$result1);
    $row2 mysql_fetch_array$result2);
    // Print out the contents of the entry 

    $id1 $row1['id'];
    $title1 $row1['title'];
    $content1 $row1['content'];
    $image1 $row1['image'];

    $id2 $row2['id'];
    $title2 $row2['title'];
    $content2 $row2['content'];
    $image2 $row2['image'];
    ?> 

  2. #2
    Join Date
    Jul 2007
    Location
    Orlando, FL
    Posts
    13

    this might make debug easier

    I am not sure if this helps; but it would seem to make debug easier.

    PHP Code:

    // Retrieve all the data from  table 1
    $result1 mysql_query("SELECT * FROM home WHERE id='1'");

    $row1 mysql_fetch_array$result1); 
               
    $id1 $row1['id']; 
            
    $title1 $row1['title']; 
          
    $content1 $row1['content']; 
            
    $image1 $row1['image']; 
     


    // Retrieve all the data from  table 2
    $result2 mysql_query("SELECT * FROM home WHERE id='2'"); 

    $row2 mysql_fetch_array$result2); 
               
    $id2 $row2['id']; 
            
    $title2 $row2['title']; 
          
    $content2 $row2['content']; 
            
    $image2 $row2['image']; 

  3. #3
    Join Date
    Jul 2007
    Posts
    357
    extract()
    PHP Code:
    $result1 mysql_query("SELECT * FROM `home` WHERE id='1'"); 
    $row1    mysql_fetch_assoc($result1);
    extract($row1);
    // do what you want with row 1

    $result2 mysql_query("SELECT * FROM `home `WHERE id='2'"); 
    $row2    mysql_fetch_assoc($result2);
    extract($row2);
    // do what you want with row 2 
    Unless you are trying to compare stuff from the first and second... Then you can change the second extract to be extract($row2, EXTR_PREFIX_SAME, 'b')
    so you vars for the second row would be $b_(column name)
    Last edited by ellisgl; 08-01-2007 at 12:42 PM.

  4. #4
    Join Date
    Nov 2006
    Posts
    250
    thankyou!!!

  5. #5
    Join Date
    Jul 2007
    Posts
    357
    No problem!

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by novemberGrey
    Is there a more effecient way to do this??
    The real trouble is your code is static. You could do this with one query and a loop.
    Code:
    SELECT * FROM `home` WHERE `id`<0 ORDER BY `id` ASC LIMIT 2

  7. #7
    Join Date
    Jul 2007
    Posts
    357
    Of course - the limit 2 may not be what he is after...

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by ellisgl
    Of course - the limit 2 may not be what he is after...
    And then again it may be!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles