rndilger Posted December 29, 2008 Share Posted December 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138732-solved-extracting-field-names-into-array/ Share on other sites More sharing options...
Maq Posted December 29, 2008 Share Posted December 29, 2008 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'] . " "; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138732-solved-extracting-field-names-into-array/#findComment-725354 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.