SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    which way is better? Getting 1 row results from array....

    I am trying to optimise my code (some of this I wrote ages ago and I am trying to implement better PHP writing practises).

    This section I am not sure whether I can make it better/quicker/cleaner

    Old version:
    PHP Code:
    $sql_text mysql_query("select 
    TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, 
    TEXT6, TEXT7, TEXT8, BUTTON1 from page_password where LANG='
    $lang' ");
    while (
    $row_text mysql_fetch_array($sql_text)) {
        
    $text1 $row_text['TEXT1'];
        
    $text2 $row_text['TEXT2'];
        
    $text3 $row_text['TEXT3'];
        
    $text4 $row_text['TEXT4'];
        
    $text5 $row_text['TEXT5'];
        
    $text6 $row_text['TEXT6'];
        
    $text7 $row_text['TEXT7'];
        
    $text8 $row_text['TEXT8'];
        
    $button1 $row_text['BUTTON1'];

    I then can use <?=$text1?> throughout my code to access the text stored.

    Improved version:
    PHP Code:
    $q_result mysql_query("select 
    TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, 
    TEXT6, TEXT7, TEXT8, BUTTON1 from page_password where LANG='
    $lang' "); 
    while (
    $ary mysql_fetch_assoc($q_result)) { 
     while (list(
    $key,$val) = each($ary)) { 
       $
    $key $val


    I again use <?=$TEXT1?> throughout code.

    Is the second version better, can I improve on this? I am only pulling out 1 row from the dB and displaying it at various places on the website page, because of this I found that I couldn't use mysql_results due to it not being 1 lump of text (e.g. I couldn't seperate it into $text1 variables).

    So am I right is it better or is there an even better way of doing it?

    Thanks

    Sarah
    Regular user

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $q_result mysql_query("select 
    TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, 
    TEXT6, TEXT7, TEXT8, BUTTON1 from page_password where LANG='
    $lang' "); 
    while (
    $ary mysql_fetch_assoc($q_result)) { 
      
    extract ($ary);

    Just a thought - don't know if it is necessarily faster than what you have. Look up the extract( ) function if the PHP manual.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks, doing 1 while loop rather than 2 must do it some good

    I must admit the PHP manual doesn't always make a great deal of sense to me - I like to see more practical examples (and not ones full of foo!)
    Regular user


Bookmarks

Posting Permissions

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