1

我需要将类中调用的数组值存储为数据库中的对象。编码

<?php
echo "BEST SELECTED POPULATION";
debug(GA::select($ga->population,'total',3)); //The best
$asma[]=GA::select($ga->population,'total',3); //The best
}
print_r($asma);
?>

$array1是我得到输出值的数组,这个数组是动态的,它的值的数量增加取决于用户输入。

 <?php
    include('config.php');
    //database connection
    //query
    $new_array = array($asma);

    foreach($new_array as $key => $value) {

       foreach ( $value as $ind => $data ) {
          /*
          You now have access to field values like this
           $data['Voltage']
           $data['Number']
           $data['Duration']
          */

          // query makes no sense 3 fields mentioned and 4 parameters given???
          // you will have to decide which of the fields from $data[] you want to load 
          // to which fields in the database.
          $sql = "INSERT INTO ga (gaid,fe,fe1,timestamp) VALUES ('', '$key', '$value', '".date("Y-m-d H:i:s")."')";
          $stmt = mysql_query($sql) or die(mysql_error());

       } // endforeach
    } // endforeach
?>

如果我使用上面的代码进行插入,它不会显示错误,但它会ga像这样在表中输入值

gaid      fe      fe1     timestamp
 1        0      array      -

上面的代码我用来在我的表中插入ga 输出print_r($asma);

Array ( 
    [0] => Array ( 
        [0] => H Object ( 
            [Voltage] => 12 
            [Number] => 1 
            [Duration] => 3 
        ) 
        [1] => H Object ( 
            [Voltage] => 26 
            [Number] => 4 
            [Duration] => 8
        ) 
        [2] => H Object ( 
            [Voltage] => 26 
            [Number] => 4 
            [Duration] => 8 
        ) 
    ) 
    [1] => Array ( 
        [0] => H Object ( 
            [Voltage] => 18 
            [Number] => 1 
            [Duration] => 4 
        ) 
        [1] => H Object ( 
            [Voltage] => 38 
            [Number] => 4 
            [Duration] => 10 
        ) 
        [2] => H Object ( 
            [Voltage] => 36 
            [Number] => 2 
            [Duration] => 8 
        ) 
    ) 
) 

我需要将数据库中的所有值存储在上面的输出 6 值中。

这是桌子

gaid    fe    fe1    fe2   timestamep
4

1 回答 1

0

您在描述中不断谈论这无济于事,array1但我没有看到array1您的代码中调用了任何内容。

但是假设这array1 = $new_array是一个建议。

<?php
    include('config.php');
    //database connection
    //query
    /*
     * This achieves nothing other than adding an extra level of array
     *  Try removing it
     */ 
    //$new_array = array_combine($asma,$asma);

    foreach($asma as $key => $value) {

       foreach ( $value as $ind => $hObject ) {
          $q = "INSERT INTO ga (fe, fe1, f2, timestamp ) VALUES (%d, %d, %d, '%s' );
          $qs = sprintf( $q, $hObject->Voltage,$hObject->Duration, 
                             $hObject->Number, date("Y-m-d H:i:s") );
          $result = mysql_query($qs);
          if ( ! $result ) {
              die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );                
          }

       } // endforeach
    } // endforeach
?>
于 2013-07-03T15:47:48.720 回答