I have a table used for checkboxes where values are either 1 or 0.
I'm having an issue printing column names for the selected row where the field value is equal to 1 (excluding any row equal to 0) and separate by comma, using implode.
I've tried several approaches but without the use of mysql_num_fields I haven't been able to print the field names with the array (mysql_fetch_array)
Here is what I am currently working with. Any help is much appreciated. And yes, I will be moving to mysqli.
$query_columns = mysql_query("SELECT field1, field2, fields3 FROM table1 WHERE user_id = '" . $id . "'");
$numberfields = mysql_num_fields($query_columns);
for ($i=0; $i<$numberfields ; $i++ ) {
$var = mysql_field_name($query_columns, $i);
$row_title .= $var;
}
echo $row_title;
echo wont print null or false values you can see that this simple example;
echo '--------------<br>';
echo false;
echo null;
echo '<br>--------------';
Also if you just want to print the field names, ie not their values? ( I haven't been able to print the field names )
Why not just do this ( assuming you have at least one result ):
$row = mysql_fetch_assoc($query_columns);
echo implode(', ', array_keys($row));
And if you don't want the 0 value names just use filter to remove them, then print:
$row = mysql_fetch_assoc($query_columns);
echo implode(', ', array_keys( array_filter( $row )));
If you want a nice print out of the $row data just do this:
echo "<pre>";
print_r($row); // or var_export($row)
echo "</pre>";