Al Hennessey
Al Hennessey

Reputation: 2445

Getting MySQL Error with Array Select

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

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

Upvotes: 1

Views: 86

Answers (4)

Johndave Decano
Johndave Decano

Reputation: 2113

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

WHERE mat_token = '$columnName'"

Upvotes: 1

Peon
Peon

Reputation: 8030

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

}

Upvotes: 0

Rhys
Rhys

Reputation: 1491

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

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

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

Upvotes: 0

Judge Mental
Judge Mental

Reputation: 5239

This expression seems wrong:

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

I think you mean

$columnName != "ext_token" && $columnName != "ext_token_child"

Upvotes: 4

Related Questions