Jump to content

A variable from a variable


liamoco

Recommended Posts

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);
}

 

Link to comment
https://forums.phpfreaks.com/topic/219611-a-variable-from-a-variable/
Share on other sites

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. 

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;
}

 

$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++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.