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" }
4

4 回答 4

4

This expression seems wrong:

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

I think you mean

$columnName != "ext_token" && $columnName != "ext_token_child"
于 2012-07-21T22:24:08.800 回答
1

您忘记用单引号将值括起来。它应该像

WHERE mat_token = '$columnName'"

于 2012-07-21T22:51:47.133 回答
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.

于 2012-07-21T22:24:48.800 回答
0

尝试这个:

$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";   
  }

}
于 2012-07-21T22:29:19.100 回答