I'm trying to move data from one or more tables to identical table(s)
in a different database, likely on a different server. This prevents
me from using INSERT..SELECT, so up until now I've done a select on
the source table and looped through the results inserting them
individually into the target table. I thought that a prepared
statement might improve the performance, but I can't get it to work.
Below is some sample code, and I imagine it's in the binding of
$arrRowData (using call_user_func_ array) that's not working. Any ideas
welcome.
<?php
$strSourceDB = "dbSource";
$strTargetDB = "dbTarget";
$objSourceDB = mysqli_connect( "", "", "", $strSourceDB);
$objTargetDB = mysqli_connect( "", "", "", $strTargetDB);
$strSourceTable = "tblA";
$strTargetTable = "tblB";
$arrRowData = array();
$strColumnNames = "";
$strParamTypes = "";
$strPlaceholder s = "";
// Get column names and their properties
$result = $objSourceDB->query("SHOW COLUMNS FROM $strSourceDB.
$strSourceTable ") or die("Query: ".$objSourc eDB->error);
while ($row = $result->fetch_row())
{
$arrRowData[] = "";
$strColumnNames .= $row[0].", ";
if (strpos($row[1], "int") || strpos($row[1], "bool"))
$strParamTypes .= "i";
elseif (strpos($row[1], "blob")) $strParamTypes .= "b";
elseif (strpos($row[1], "big") || strpos($row[1], "double"))
$strParamTypes .= "d";
else $strParamTypes .= "s";
$strPlaceholder s .= "?, ";
}
$strColumnNames = substr($strColu mnNames, 0, strlen($strColu mnNames)
- 2);
$strPlaceholder s = substr($strPlac eholders, 0,
strlen($strPlac eholders) - 2);
// Create prepared statement
$objStatement = $objTargetDB->stmt_init() or die($objStateme nt-
$objStatement->prepare("INSER T INTO $strTargetDB.$s trTargetTable
($strColumnName s) VALUES ($strPlaceholde rs)") or die("Prepare: ".
$objStatement->error);
array_unshift($ arrRowData, $strParamTypes) ;
if (!call_user_fun c_array(array(& $objStatement, "bind_param "),
$arrRowData)) die("Bind Param: ".$objState ment->error);
array_shift($ar rRowData);
// Select and Insert rows
$objResult = $objSourceDB->query("SELEC T $strColumnNames FROM
$strSourceDatab ase.$strSourceT able WHERE DateTime <=
DATE_SUB(CURDAT E(), INTERVAL 90 DAY)") or die("Query: ".$objSourc eDB-
while ($arrRowData = $objResult->fetch_row())
{
$objStatement->execute() or die("Execute: ".$objState ment-error);
}
?>
in a different database, likely on a different server. This prevents
me from using INSERT..SELECT, so up until now I've done a select on
the source table and looped through the results inserting them
individually into the target table. I thought that a prepared
statement might improve the performance, but I can't get it to work.
Below is some sample code, and I imagine it's in the binding of
$arrRowData (using call_user_func_ array) that's not working. Any ideas
welcome.
<?php
$strSourceDB = "dbSource";
$strTargetDB = "dbTarget";
$objSourceDB = mysqli_connect( "", "", "", $strSourceDB);
$objTargetDB = mysqli_connect( "", "", "", $strTargetDB);
$strSourceTable = "tblA";
$strTargetTable = "tblB";
$arrRowData = array();
$strColumnNames = "";
$strParamTypes = "";
$strPlaceholder s = "";
// Get column names and their properties
$result = $objSourceDB->query("SHOW COLUMNS FROM $strSourceDB.
$strSourceTable ") or die("Query: ".$objSourc eDB->error);
while ($row = $result->fetch_row())
{
$arrRowData[] = "";
$strColumnNames .= $row[0].", ";
if (strpos($row[1], "int") || strpos($row[1], "bool"))
$strParamTypes .= "i";
elseif (strpos($row[1], "blob")) $strParamTypes .= "b";
elseif (strpos($row[1], "big") || strpos($row[1], "double"))
$strParamTypes .= "d";
else $strParamTypes .= "s";
$strPlaceholder s .= "?, ";
}
$strColumnNames = substr($strColu mnNames, 0, strlen($strColu mnNames)
- 2);
$strPlaceholder s = substr($strPlac eholders, 0,
strlen($strPlac eholders) - 2);
// Create prepared statement
$objStatement = $objTargetDB->stmt_init() or die($objStateme nt-
>error);
($strColumnName s) VALUES ($strPlaceholde rs)") or die("Prepare: ".
$objStatement->error);
array_unshift($ arrRowData, $strParamTypes) ;
if (!call_user_fun c_array(array(& $objStatement, "bind_param "),
$arrRowData)) die("Bind Param: ".$objState ment->error);
array_shift($ar rRowData);
// Select and Insert rows
$objResult = $objSourceDB->query("SELEC T $strColumnNames FROM
$strSourceDatab ase.$strSourceT able WHERE DateTime <=
DATE_SUB(CURDAT E(), INTERVAL 90 DAY)") or die("Query: ".$objSourc eDB-
>error);
{
$objStatement->execute() or die("Execute: ".$objState ment-error);
}
?>
Comment