master82 Posted September 5, 2006 Share Posted September 5, 2006 Here is a sample of code I use to get data from my users table, as you can see its very long and in some other tables there are alot more fields - so it very time consuming[code]include("connectionfile.php");$values = "SELECT field, field1, field2, field3, field4, field5, field6, field7, field8, field9 FROM users WHERE field1 = '$anothervalue';$output = mysql_query($values,$connection) or die("Unable to retrieve user data");while ($newArray = mysql_fetch_array($output)){$field1= $newArray[''field1];$field2= $newArray['field2'];$field3= $newArray['field3'];$field4= $newArray['field4'];$field5= $newArray['lfield5'];$field6= $newArray['field6'];$field7= $newArray['field7'];$field8= $newArray['field8'];$field9= $newArray['field9'];}[/code]when I require one of these fields I simply[code]echo"$field1";[/code]It works fine but I've seen it done a shorter way, but couldnt get it to work:[code]$values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");while($us=mysql_fetch_array($values)) {}[/code]I thought I could then use the following to call the data:[code]echo"$us[field1]";[/code]...but the values came out blank :-[Any idea where im going wrong or a different way to do this? Link to comment https://forums.phpfreaks.com/topic/19751-data-variables-shortcut/ Share on other sites More sharing options...
shocker-z Posted September 5, 2006 Share Posted September 5, 2006 NOT tested but this should work or a less i have made a stupid litle mistake somewhere...[code]<?php $values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");$us=mysql_fetch_array($values);$i=1;$ii='field1';while ($us[$ii] !== '') { $i++; $$ii=$us[$ii]; $ii='field'.$i;}?>[/code]notice how i didnt use a while for the data being pulled out.. this is because you only have 1 line of data in your database else if you have more then you will have to put everything into an array like below..[code]<?php $values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");while ($us=mysql_fetch_array($values)) { $i=1; $ii='field1'; while ($us[$ii] !== '') { $i++; $$ii[]=$us[$ii]; $ii='field'.$i; }}?>[/code]then you can call each filed1,2,3,4.... by using $file1[0] which will pull out the data of field 1 row 1...RegardsLiam Link to comment https://forums.phpfreaks.com/topic/19751-data-variables-shortcut/#findComment-86283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.