我有一个巨大的 json 数组。 json 数组的某些元素本身就是序列化的 json 数组。问题是我需要使用这个大型 json 数组进行大量的表插入。我也在使用pdo
。
如何使用pdo
快速将这个本身已序列化json数组的json数组转换为表插入?
这似乎花费了太多时间,而且似乎不是一种干净的做事方式。
基本上,我有一个使用 json 数组插入表的接口(interface),我需要使用 pdo 的列和占位符将其转换为表插入。数组中需要插入的元素太多。
我尝试使用此代码自动创建插入,它工作得很好,而且我喜欢它的结果,但我无法使用此代码或使用其元素本身已序列化 json 数组的 json 数组来自动化它.
function create_query( $table_name, $cols, $placeholders )
{
$query= "INSERT INTO $table_name (
$cols
)
VALUES (
$placeholders
)";
}
/*
arg1= table_name;
*/
function insert_into_table( $argums, $location )
{
$num_classes= $location[ "num_classes" ];
foreach( $location as $key => $value )
{
$$key= $value;
}
$keys= array_keys( $argums );
$table_name = $keys[ 0 ];
array_shift( $keys );
array_shift( $argums );
$cols= implode( ", ", $keys );
$bound_array= array();
echo var_dump( $keys );
foreach( $keys as &$value )
{
$bound_array[ $value ]= ${ $argums[ $value ] };
$value= ":".$value;
}
$placeholders= implode( ", ", $keys );
$query= create_query( $table_name, $cols, $placeholders );
return array( "query" => $query, "bind" => $bound_array );
}
function do_insert( $arr )
{
global $db;
echo var_dump( $arr );
$query= $arr[ "query" ];
$bind= $arr[ "bind" ];
$stmt= $db->prepare( $query );
echo var_dump( $stmt );
if( !$stmt->execute( $bind ) )
{
echo var_dump( $stmt->errorInfo() );
}
$affected_rows= $stmt->rowCount();
}
$assoc_arr= insert_into_table( array( "company" => "company", 'companyName'=> "companyName", 'pkid' => 'pkid', 'reminderEmailTop' => 'reminderEmailTop', 'reminderEmailBottom' => 'reminderEmailBottom', 'reminderLink' => 'reminderLink' ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "basiclife_type", "page_nav" => "basiclife_pagenav", "orderby" => "basiclife_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "bene_type", "page_nav" => "bene_pagenav", "orderby" => "bene_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "beneper_type", "page_nav" => "beneper_pagenav", "orderby" => "beneper_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "dent_type", "page_nav" => "dent_pagenav", "orderby" => "dent_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "eflex_type", "page_nav" => "eflex_pagenav", "orderby" => "eflex_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "hsa_type", "page_nav" => "hsa_pagenav", "orderby" => "hsa_orderby" ), $location );
do_insert( $assoc_arr );
...以及大约 50 多个此类插入物..
但是,对于包含本身就是 json 数组的元素的插入,我无法使用上面的代码。
所以我创建了一个函数,它只是一个手动输入的插入...
最佳答案
这是一个原型(prototype)。只需向其提供数据数组、csv 或 json 文件,它就会运行所有数据条目。根据需要调味: MySql 类 { 私有(private) $sDbName = '玩'; 私有(private) $sUsername = 'root'; 私有(private) $sPassword = ''; 私有(private) $sHost = '本地主机'; 私有(private) $oConnection = null;
public function __construct()
{
$this->oConnection = new PDO(
'mysql:host='
. $this->sHost
. ';dbname='
. $this->sDbName,
$this->sUsername,
$this->sPassword
);
}
public function getDb()
{
return $this->oConnection;
}
public function bindVariables( &$oStmp, $aBinds )
{
foreach( $aBinds as $sVariable => $vValue )
{
// Ensure we have a colon prepended for PDO.
if( substr( $sVariable, 0, 1 ) !== ':' )
{
$sVariable = ':' . $sVariable;
}
$oStmp->bindValue( $sVariable, $vValue );
}
}
}
$oMySql = new MySql;
$oDb = $oMySql->getDb();
// $sCsvExampleLine = '1,2,3,4,5';
$aCsv = file( 'infile6.csv' );
$iCountlines = count( $aCsv );
for( $j = 0; $j < $iCountlines; ++$j )
{
$aRaw = explode( ',', $aCsv[ $j ] );
$aData[ $j ][ 'companyNav' ] = $aRaw[ 0 ];
$aData[ $j ][ 'companyId' ] = $aRaw[ 1 ];
$aData[ $j ][ 'type' ] = $aRaw[ 2 ];
$aData[ $j ][ 'page_nav' ] = $aRaw[ 3 ];
$aData[ $j ][ 'orderby' ] = $aRaw[ 4 ];
}
// $aData[] = array( 'companyNav' => '1', 'companyId' => '2', 'type'=> '3', 'page_nav' => '4', 'orderby' => '5' );
// Create the sql outside of the loop.
$sSql = "
INSERT INTO `1`
( companyNav, companyId, type, page_nav, orderby )
VALUES
( :companyNav, :companyId, :type, :page_nav, :orderby )
";
$oStmp = $oDb->prepare( $sSql );
$iCountData = count( $aData );
for( $k = 0; $k < $iCountData; ++$k )
{
$oMySql->bindVariables( $oStmp, $aData[ $k ] );
$oStmp->execute();
// var_dump( $oStmp->rowCount() );
}
$oErrors = $oStmp->errorInfo();
var_dump( $oErrors );
可以利用绑定(bind)来使用相同的 sql 插入不同的数据。 上面的查询允许您将数据库连接包装在类中以方便使用,并避免使用全局变量。
O/P 主要关心的是复制插入的艰巨任务。如果能够以正确的顺序/格式从外部源中提取数据或按需生成数据,那么这项任务的开销就会减少。
只要格式保持不变,您就可以继续从各种来源附加到 $aData,它将根据需要插入。
可以通过实现多个绑定(bind)来进一步改进以下内容,但这稍微超出了问题的范围,即使效率更高。
关于php - 使用 pdo 将大型 json 数组插入表中。 --> 永远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056123/