liamoco Posted November 23, 2010 Share Posted November 23, 2010 Is this possible? (this maybe confusing). I have a for loop to get table field names as shown below. Lets say one of the field names is called `users`. How can I make a variable called $users. $cols = 13; for($x=1;$x<$cols;$x++) { $infoType = mysql_field_name(mysql_query("SELECT * FROM profileInfo_choose WHERE user_id='{$username_id}'"), $x); } Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/ Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 $sql = "SELECT * FROM profileInfo_choose WHERE user_id='{$username_id}'"; $result = mysql_query($sql) or die(mysql_error()); while ($data = mysql_fetch_assoc($result)) { extract($data); echo "users: $users <br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138576 Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 but if you literally want to create a variable name from a variable, you can do this: $some_val = "helloworld"; ${$some_val} = "goodbye"; echo $helloworld; output: goodbye Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138579 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 liamoco, while it is possible that the code you posted is just an example, if your real code is nesting a mysql_query() like that and then using a loop to execute that code several time, please be advised that doing that is a processing hog and will make your web host unhappy with you. You should not execute the same query in a loop (or even related queries that differ in only what they retrieve) and you should not nest a mysql_query() in another function call since it prevents using any error checking logic that a real application needs. Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138585 Share on other sites More sharing options...
liamoco Posted November 23, 2010 Author Share Posted November 23, 2010 Thank you for the advice PFMaBiSmAd but I cannot get myself to think of another way to write this. Could you please modify it an explain, I'll be truly grateful. $cols = 13; for($x=1;$x<$cols;$x++) { $infoType = mysql_field_name(mysql_query("SELECT * FROM profileInfo_choose WHERE user_id='{$username_id}'"), $x); ${$infoType} = $infoType; } Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138607 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 $query = "SELECT * FROM profileInfo_choose WHERE user_id='{$username_id}'"; $result = mysql_query($query); $i = 0; $num_fields = mysql_num_fields($result); while ($i < $num_fields){ $field_name = mysql_field_name($result, $i); // your code that uses the field name here... $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138637 Share on other sites More sharing options...
liamoco Posted November 23, 2010 Author Share Posted November 23, 2010 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/#findComment-1138645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.