Jump to content

[SOLVED] extracting field names into array


Recommended Posts

Hello, I have a table from which I'd like to extract specific field names, only those that are prefixed with 'mem_'. These columns represent the different types of memberships allowed in an organization. The rest of the columns in the table hold information regarding the individual member (e.g., name, address, etc). My intent is to output the column names into a php array for use in other code, but I need a clean array of just these column headings. Here is the code I have so far:

 

$field_names = array();
$cols = mysql_query("SHOW COLUMNS FROM `map`");
while($fields = mysql_fetch_assoc($cols))
{
$f = $fields['Field'];
if(substr(0,4) == "mem_")
{
	$field_names[] = $f;
}
}
print_r($field_names);

 

This code just prints 'Array' over and over.

 

Any advice?

 

Thanks in advance.

 

Ryan

You never specify what string you want to take a sub string of.

 

Try this and see if the correct columns print out:

 

$field_names = array();
$result = mysql_query("SHOW COLUMNS FROM map") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
	if(substr($row['Field'], 0, 4) == "mem_") {
		$field_names[] = $row['Field'];
		echo "Just added: " . $row['Field'] . "
";
	} else {
		echo "Didn't add: " . $row['Field'] . "
";
	}
    }
}
?>

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.