1

I am having trouble with one of my arrays. For some reason, whenever I test it I get this error:

  • Unknown column 'ext_token' in 'where clause' if I have the or die mysql error on,
  • or I get Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given, if I take it out.

I’m not sure whether it has something to do with the fact that the first columnName in the array is ext_token, but I thought I would get rid of that by the if below.

Here is the code for the array:

   $mat_total = array();

foreach ($result_array1 as $columnName => $columnData){

   if($columnName != "ext_token" || "ext_token_child"){
           var_dump($columnName);
           $mat_sql = mysql_query("SELECT * FROM materials WHERE mat_token = $columnName");
           $mat_array = mysql_fetch_assoc($mat_sql);
           $material_tok = $mat_array['mat_token'];
           $material_price_unit = $mat_array['material_price_per_unit'];


           $total_mat_price = $material_price_unit * $columnData;

           array_push($mat_total, "$material_tok => $total_mat_price");

   }
   else{
       echo "hello";    
   }

}

Thanks for any and all help.

Edit: in terms of the array here is a clearer version of it

Column name: ext_token Column data: roof
Column name: ext_token_child Column data: felt
Column name: concrete Column data: 4
Column name: cement Column data: 3
Column name: sand Column data: 2
Column name: wood_4_2 Column data: 4
Column name: wood_8_2 Column data: 2
Column name: felt Column data: 2

or there is this:

array(8) { ["ext_token"]=> string(4) "roof" ["ext_token_child"]=> string(4) "felt" ["concrete"]=> string(1) "4" ["cement"]=> string(1) "3" ["sand"]=> string(1) "2" ["wood_4_2"]=> string(1) "4" ["wood_8_2"]=> string(1) "2" ["felt"]=> string(1) "2" }
0

4 Answers 4

4

This expression seems wrong:

$columnName != "ext_token" || "ext_token_child"

I think you mean

$columnName != "ext_token" && $columnName != "ext_token_child"
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to enclose the value with single quotes. It should be like

WHERE mat_token = '$columnName'"

Comments

0

if($columnName != "ext_token" || "ext_token_child"){

Should be if($columnName != "ext_token" && $columnName != "ext_token_child"){

Or use ==, whichever you're shooting for.

Comments

0

Try this:

$mat_total = array();

foreach ( $result_array1 as $columnName => $columnData ) {

  if( $columnName != "ext_token" || $columnName == "ext_token_child" ) {

    var_dump( $columnName );
    $mat_sql = mysql_query( "SELECT * FROM materials WHERE mat_token = '$columnName'" );

    $mat_array = mysql_fetch_assoc( $mat_sql );
    $material_tok = $mat_array[ 'mat_token' ];
    $material_price_unit = $mat_array[ 'material_price_per_unit' ];

    $total_mat_price = $material_price_unit * $columnData;

    array_push( $mat_total, "$material_tok => $total_mat_price" );

  } else {
    echo "hello";   
  }

}

1 Comment

Change $columnName = "ext_token_child" to $columnName == "ext_token_child"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.