Jump to content

[SOLVED] extracting field names into array


rndilger

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'] . "
";
	}
    }
}
?>

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.