www.webdeveloper.com
Results 1 to 8 of 8

Thread: Alternative to "SELECT *"

  1. #1
    Join Date
    Feb 2010
    Location
    Grantham, UK
    Posts
    103

    Alternative to "SELECT *"

    Am having lots of questions pop up, so am posting them as I think them..hope you don't mind

    Having read that ideally, you should not use SELECT * and specify which columns you want instead, e.g.

    Code:
    SELECT column1, column2 FROM...
    I have successfully changed all my coding accordingly with one exception:


    Code:
    while ($gptp <=5)
    	{
    	$gptplan="plan".$gptp;
    	$gptget = mysql_query("SELECT * FROM $gptplan
    		WHERE userid = '$gptuser' AND email = '$gptemail' LIMIT 1");
    		while($row = mysql_fetch_array($gptget))
    		{
    			$_SESSION[plantot][$gptp]=$row[plantot];
    			$_SESSION[plannew][$gptp]=$row[plannew];
    			$gptlvl=1;
    			while ($gptlvl <=9)
    			{
    				$gpttot="lvl".$gptlvl."tot";
    				$gptnew="lvl".$gptlvl."new";
    				$_SESSION[gpttot][$gptp][$gptlvl]=$row[$gpttot];
    				$_SESSION[gptnew][$gptp][$gptlvl]=$row[$gptnew];
    				$gptlvl++;
    			}
    		}
    	$gptp++;	
    	}

    Any ideas how I could work this one? Or am I allowed to occasionally have an exception to the rule?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,244
    Looks to me like you use 4 columns -- just look at which array indexes are used with $row -- so you could make them the columns used in your SELECT. (Note that if those are the only 4 columns in that table, then it's not bad performance-wise to just use "*", though that makes your source code a bit less readable.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Are these ;

    $_SESSION[gpttot][$gptp][$gptlvl]=$row[$gpttot];
    $_SESSION[gptnew][$gptp][$gptlvl]=$row[$gptnew];

    supposed to be

    $_SESSION[gpttot][$gptp][$gptlvl]=$row[gpttot];
    $_SESSION[gptnew][$gptp][$gptlvl]=$row[gptnew];

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,244
    Quote Originally Posted by Dasher View Post
    Are these ;

    $_SESSION[gpttot][$gptp][$gptlvl]=$row[$gpttot];
    $_SESSION[gptnew][$gptp][$gptlvl]=$row[$gptnew];

    supposed to be

    $_SESSION[gpttot][$gptp][$gptlvl]=$row[gpttot];
    $_SESSION[gptnew][$gptp][$gptlvl]=$row[gptnew];
    I didn't notice that before, but probably explains the OP's question: it looks like the table must have a bunch of columns with names like lvl1new, lvl1tot, lvl2new, lvl2tot, etc.... Now I'm sort of leaning toward a DB redesign to move those enumerated columns into one or more separate tables with a foreign key into the main table.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Feb 2010
    Location
    Grantham, UK
    Posts
    103
    Thankyou for your help.

    NogDog - you are correct. There are approx 18 columns being called in each table (5 of them) out of 20 columns. The other 2 being my verification (userid and email)

    It would be possible to list them all in the SELECT, but would make reading very difficult. If I am calling all but two of the columns, would that be acceptable use of the *?

    I have heard of foreign keys before, but as yet have had no introduction, so do not understand them yet.


    As an additional question, I am also going to have to set all the lvl1new, lvl2new etc... to 0 upon each login. Is there any way of doing this as within the original query, or do I have to run it as a separate UPDATE query? (which is what i am expecting)
    Last edited by max2474; 05-20-2012 at 07:03 AM. Reason: addition

  6. #6
    Join Date
    May 2012
    Posts
    5
    having lots of questions pop up, so am posting them as I think them..hope you don't mind

    Having read that ideally, you should not use SELECT * and specify which columns you want instead

    By
    best dividend stocks

  7. #7
    Join Date
    Feb 2010
    Location
    Grantham, UK
    Posts
    103
    interesting cna....? :/

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,244
    Quote Originally Posted by max2474 View Post
    interesting cna....? :/
    Would you like some ketchup with that spam?

    (I've reported the post in question, so hopefully before long late-comers to this thread will wonder what you were talking about.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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