Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将类对象数组值存储到数据库中并从数据库中检索这些值_Php_Html_Phpmyadmin - Fatal编程技术网

Php 如何将类对象数组值存储到数据库中并从数据库中检索这些值

Php 如何将类对象数组值存储到数据库中并从数据库中检索这些值,php,html,phpmyadmin,Php,Html,Phpmyadmin,我在一个有动态数组的类中工作。我需要将类的这些动态数组的结果存储在数据库中 <?php require_once('ag.php'); class H { var $Voltage; var $Number; var $Duration; function H($Voltage=0,$Number=0,$Duration=0) { $this->Voltage = $Voltage; $this->Number

我在一个有动态数组的类中工作。我需要将类的这些动态数组的结果存储在数据库中

<?php
require_once('ag.php');
class H
 {
    var $Voltage;
    var $Number;
    var $Duration;
function H($Voltage=0,$Number=0,$Duration=0)
     {
        $this->Voltage = $Voltage;
        $this->Number = $Number;
        $this->Duration = $Duration;
    }}
//This will be the crossover function. Is just the average of all properties.
function avg($a,$b) {
return round(($a*2+$b*2)/2);
}
//This will be the mutation function. Just increments the property.
function inc($x)
 {
    return $x+1*2;
}
//This will be the fitness function. Is just the sum of all properties.

function debug($x) 
{
    echo "<pre style='border: 1px solid black'>";
    print_r($x);
    echo '</pre>';
    }
//This will be the fitness function. Is just the sum of all properties.
function total($obj) 
{
return $obj->Voltage*(-2) + $obj->Number*2 + $obj->Duration*1;
}
$as=array();
for($i=0;$i<$row_count;$i++) 
{
$adam = new H($fa1[$i],$fb1[$i],$fcc1[$i]);
$eve = new H($fe1[$i],$ff1[$i],$fg1[$i]);
$eve1 = new H($fi1[$i],$fj1[$i],$fk1[$i]);
$ga = new GA();
echo "Input"; 
$ga->population = array($adam,$eve,$eve1);
debug($ga->population);
$ga->fitness_function = 'total';    //Uses the 'total' function as fitness function
$ga->num_couples = 5;               //4 couples per generation (when possible)
$ga->death_rate = 0;                //No kills per generation
$ga->generations = 10;              //Executes 100 generations
$ga->crossover_functions = 'avg';   //Uses the 'avg' function as crossover function
$ga->mutation_function = 'inc';     //Uses the 'inc' function as mutation function
$ga->mutation_rate = 20;            //10% mutation rate
$ga->evolve();                      //Run
echo "BEST SELECTED POPULATION";
debug(GA::select($ga->population,'total',3)); //The best
$as=array((GA::select($ga->population,'total',3))); //The best
}
?>


$as
是一个数组,其中包含我需要存储在数据库中的值。请在这方面帮助我。此
$as
数组包含计算后的
$adam、$eve、$eve1
值。

要在数据库中存储数组,可以使用PHP函数

然后,当您从数据库检索它时,请使用


还考虑使用PHP5符号进行类声明,例如声明函数和成员变量AS,并使用<代码>函数而不是

函数您的\u类\u名称

先生,我如何编写该名称的查询?输入列名称显示我可以编写该名称的查询?有关mysql函数的更多详细信息,字段名称是一个或数组中的输出数$ass意味着如果您序列化数组或对象您将只获得一个值如何在数据库中存储9输出值
<?php 

//serialize  and store the value into the database

//however to storing the object into the db is not good practice
$val = serialize($$as);

$sql = "insert into table_name field_name values('$val')";

//execute the query 
$query = mysqli_query($connection_variable, $sql) or die('error in query');


//while retrieving use unserialize($str) function
unserialize($val);
$back_to_array = unserialize($result_from_database); //You now have the array again
<?php 

//serialize  and store the value into the database

//however to storing the object into the db is not good practice
$val = serialize($$as);

$sql = "insert into table_name field_name values('$val')";

//execute the query 
$query = mysqli_query($connection_variable, $sql) or die('error in query');


//while retrieving use unserialize($str) function
unserialize($val);