Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternatives to "SELECT *" 1

Status
Not open for further replies.

max2474

Programmer
May 10, 2012
40
GB
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.

PHP:
SELECT column1, column2 FROM...

I have successfully changed all my coding accordingly with one exception:

PHP:
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?
 
I guess its possible.
There are about 20 columns being called in each table... out of 22.
lvl1tot
lvl1new
lvl2tot
lvl2new
etc..


I guess I should have mentioned that. If I am calling pretty much all columns, is that an acceptable use of the *, or should you really still list all the columns?
 
Hi

max2474 said:
Having read that ideally, you should not use SELECT * and specify which columns you want instead
True. I also used to care about this, especially in case of [tt]join[/tt]ed tables. But as you wrote, "ideally". I mean, I not take this as rule and not let it make neither my SQL or PHP code a mess just to keep the SQL ideal.

max2474 said:
Any ideas how I could work this one?
I would use a array, but this goes out of this forum's topic :
Code:
[highlight][navy]$field[/navy][teal]=[/teal][b]array[/b][teal]([/teal][/highlight]
  [highlight][green][i]'table1'[/i][/green][teal]=>[/teal][green][i]'field11,field12'[/i][/green][teal],[/teal][/highlight]
  [highlight][green][i]'table2'[/i][/green][teal]=>[/teal][green][i]'field21'[/i][/green][teal],[/teal][/highlight]
  [highlight][green][i]'table3'[/i][/green][teal]=>[/teal][green][i]'field31,field32,field33'[/i][/green][teal],[/teal][/highlight]
[highlight][teal]);[/teal][/highlight]

[b]while[/b] [teal]([/teal][navy]$gptp[/navy] [teal]<=[/teal][purple]5[/purple][teal])[/teal]
	[teal]{[/teal]
	[navy]$gptplan[/navy][teal]=[/teal][green][i]"plan"[/i][/green][teal].[/teal][navy]$gptp[/navy][teal];[/teal]
	[navy]$gptget[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][green][i]"SELECT [highlight]$field[$gptp][/highlight] FROM $gptplan[/i][/green]
[green][i]		WHERE userid = '$gptuser' AND email = '$gptemail' LIMIT 1"[/i][/green][teal]);[/teal]


Feherke.
[link feherke.github.com/][/url]
 
Thanks for the idea. I guess its a case of weighing up the choices, though I will look into the idea of using an array as didn't consider that option. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top