patternsqlMinor
Skip specific field name
Viewed 0 times
fieldnameskipspecific
Problem
I am trying to execute a table from a MySQL database using a
Here is my code:
As you can see, I have an if statement
In this case I need help on how to skip a specific field name, and also the field rows of this field name.
Note: I used loop statement in this because the table field name from the database is not fixed. For example the user wants to add new columns. So I can't just code it manually because the table doesn't have a fixed data.
for loop statement, in order to get the field names as the column header.Here is my code:
$result = mysql_query("SELECT * FROM bom_commonmaterials where description like '%CAP%'");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "";
// printing table headers
for($i=0; $i{$field->name}";
if ($field ="WATTAGE")
{
CONTINUE;
}
}
echo "\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell";
echo "\n";
}
mysql_free_result($result);
echo "";As you can see, I have an if statement
$field ="WATTAGE" inside the for loop statement. I am trying to execute this code so that the field name WATTAGE will be not included as a table header, but I think my code is wrong.In this case I need help on how to skip a specific field name, and also the field rows of this field name.
Note: I used loop statement in this because the table field name from the database is not fixed. For example the user wants to add new columns. So I can't just code it manually because the table doesn't have a fixed data.
Solution
You should have an IF ELSE in your
You are assigning
before you get to the code
WATTAGE has has already been assigned to
Put an IF/ELSE in your
for loop:for($i=0; $i<$fields_num; $i++)You are assigning
$field to the string WATTAGE here:$field = mysql_fetch_field($result);
echo "{$field->name}";before you get to the code
echo "{$field->name}";
if ($field ="WATTAGE")
{
CONTINUE;
}WATTAGE has has already been assigned to
$field before you get to if($field = "WATTAGE") code.Put an IF/ELSE in your
for loop and then put the CONTINUE; bit in the IF and the assignment in the ELSE part. That way you'll never get to the $field assignment if the fieldname is WATTAGE.Code Snippets
for($i=0; $i<$fields_num; $i++)$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";echo "<th>{$field->name}</th>";
if ($field ="WATTAGE")
{
CONTINUE;
}Context
StackExchange Database Administrators Q#106530, answer score: 4
Revisions (0)
No revisions yet.