Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.