如何插入动态数组在PHP数据库? [英] How to insert dynamic array into the database in php?
问题描述
我正在动态数组上工作,我需要插入的database.when这些阵列我插入动态数组到数据库中,而不是在一行的封面三排显示的。
编码插入
这是我的数组$阿斯玛[] = GA ::选择($ GA->人口,'总',3);
这是插入查询,我需要在六个栏插入细节作为输出包括六个值的。
$电压=阵列();
$持续时间=阵列();
$数=阵列(); 的foreach($阿斯玛为$关键=> $值)
{ 的foreach($值$ IND = GT; $ hObject)
{
$电压[] = $ hObject->电压;
$时间[] = $ hObject->持续时间;
$号[] = $ hObject->数量; }} // endforeach
对于(i = 0; I< ROW_COUNT;我++)
{
$ Q =INSERT INTO GA(FE,FE1,FE2,的Fe3,FE4,FE5,FE6,F7,F8,时间戳,用户名)VALUES(%D,%D,%D,%D,%D,%D, %D,%D,%D,'%s'的,'$ login_session');
$ QS = sprintf的($ Q $电压[$ i],$时间[$ i],$号[$ i], 日期(Y-M-D H:I:S));
$结果= mysql_query($ QS);
如果(!$结果){
死(插入失败mysql_errno()''mysql_error());
}}}
?>
我需要存储在六个值有一个
行,如果用户从选项中选择 1
。如果用户从选项中选择两个
然后两个
行包括在数据库中。
这里是我的输出
阵列
(
[0] => ^ h对象
(
[电压] => 18
[编号] => 1
[时间] => 6
) [1] => ^ h对象
(
[电压] => 38
[编号] => 4
[时间] => 14
) [2] => ^ h对象
(
[电压] => 38
[编号] => 4
[时间] => 14
)
这是店这样的数据库
Volatge持续数Volatge持续数Volatge时间数
18 6 1 18 6 1 18 6 1
38 14 4 38 14 38 4 14 4
38 14 4 38 14 38 4 14 4
我需要存储这样的
Volatge持续数Volatge持续数Volatge时间数
18 6 1 38 4 14 38 14 4
plz帮助我在这个
< PHP
没有测试// code检查//逻辑代替改变为循环多次
$数据=阵列();$ J = 0;的foreach($阿斯玛为$关键=> $值)
{
$ I = 0;的foreach($值$ IND = GT; $ hObject)
{
如果($我== 0)
{
$数据[$ J] ['铁'] =函数mysql_escape_string($ hObject->电压); }其他{ $数据[$ J] ['FE'$ I] =函数mysql_escape_string($ hObject->电压);
} $ I ++; $数据[$ J] ['FE'$ I] =函数mysql_escape_string($ hObject->持续时间);
$ I ++; $数据[$ J] ['FE'$ I] =函数mysql_escape_string($ hObject->号码);
$ I ++;}$ J ++;} // endforeach//多个数组
的foreach($作为数组$的数据)
{
//独特的阵
// $ ARRAY3 = array_merge($电压,$时间,$数);$字段=破灭(',',array_keys($阵列));//如果你想添加任何新的领域追加它
。$字段=',''时间戳,用户名';$瓦尔斯='.implode(,,array_values($阵列))。';//如果你想添加任何新值追加它
$瓦尔斯=,,$ login_session。'。时间()。';
$ Q =INSERT INTO嘎VALUES($领域。)($丘壑。);$结果= mysql_query($ Q);
如果(!$结果){
死(插入失败mysql_errno()''mysql_error());
}
}
i am working on dynamic array i need to insert these array in the database.when i insert dynamic array into the database instead of displaying in one row its cover three row.
coding for insertion
this is my array
$asma[]=GA::select($ga->population,'total',3);
this is insertion query i need to insert the detail in six column as my output comprises of six values .
$Voltage = array();
$Duration = array();
$Number = array();
foreach($asma as $key => $value)
{
foreach ( $value as $ind => $hObject )
{
$Voltage[] = $hObject->Voltage;
$Duration[] = $hObject->Duration;
$Number[] = $hObject->Number;
} }// endforeach
for(i=0;i<row_count;i++)
{
$q = "INSERT INTO ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) VALUES (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";
$qs = sprintf( $q, $Voltage[$i],$Duration[$i],$Number[$i],
date("Y-m-d H:i:s") );
$result = mysql_query($qs);
if ( ! $result ) {
die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );
}}}
?>
i need to store six values in one
row if user select 1
from the option. if user select two
from the option then two
rows cover in the database.
here is my output
Array
(
[0] => H Object
(
[Voltage] => 18
[Number] => 1
[Duration] => 6
)
[1] => H Object
(
[Voltage] => 38
[Number] => 4
[Duration] => 14
)
[2] => H Object
(
[Voltage] => 38
[Number] => 4
[Duration] => 14
)
it is store in the database like this
Volatge Duration Number Volatge Duration Number Volatge Duration Number
18 6 1 18 6 1 18 6 1
38 14 4 38 14 4 38 14 4
38 14 4 38 14 4 38 14 4
i need to store like this
Volatge Duration Number Volatge Duration Number Volatge Duration Number
18 6 1 38 4 14 38 14 4
plz help my in this
<?php
//code not tested check it
//Logic is changed instead of for looping many times
$data = array();
$j = 0;
foreach($asma as $key => $value)
{
$i = 0;
foreach ( $value as $ind => $hObject )
{
if($i==0)
{
$data[$j]['fe'] = mysql_escape_string($hObject->Voltage);
}else{
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Voltage);
}
$i++;
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Duration);
$i++;
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Number);
$i++;
}
$j++;
}// endforeach
//multiple array
foreach($data as $array)
{
//unique array
//$array3 = array_merge($Voltage,$Duration,$Number);
$fields = implode(',',array_keys($array));
//if you want append any new field append it
$fields .= ','.'timestamp,username';
$vals = "'".implode("','",array_values($array))."'";
//if you want append any new values append it
$vals .= ",'".time()."','".$login_session."'";
$q = "INSERT INTO ga (".$fields.") VALUES(".$vals.")";
$result = mysql_query($q);
if ( ! $result ) {
die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );
}
}
这篇关于如何插入动态数组在PHP数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!