Here is the script
- Code: Select all
-- connection to the database works fine ----
$table1 = source table;
$table2 = target table;
// fetch data from source table
$sql = sprintf("SELECT * FROM %s", $table1);
$rslt = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));
if (mysqli_num_rows($rslt) > 0) {
while($row = mysqli_fetch_array($rslt, MYSQLI_ASSOC)) {
$field = $row['data'];
if (isset($data_array)) unset($data_array);
$data_array['data'] = $field;
// apply insert function
insert($localDatabase, $table2, $data_array);
}
}
// check the target table
$sql= sprintf("SELECT * FROM %s", $table2);
$rslt = mysqli_query($dbc, $sql) or die ("ERROR: " . mysqli_error($dbc) . " (query is $sql)");
$num = mysqli_affected_rows($dbc);
printf("mysqli_affected_rows num %s", $num);
printf("<br />");
// function to insert into table
function insert($database, $table, $data_array) {
// connection
require(DB);
// is array
if (!is_array($data_array)) die ("not array");
// chain the data string
foreach($data_array AS $k => $v) {
printf("%s => %s <br />", $k, $v);
$fieldString .= $k . ",";
$valueString .= "'" . $v . "',";
}
$fieldString = substr($fieldString, 0, -1);
$valueString = substr($valueString, 0, -1);
// insert
$sql = sprintf("INSERT INTO %s (" . $fieldString . ") VALUES (" . $valueString . ")", $table);
mysqli_query($dbc, $sql);
}
?>
When run it in the desktop's harddrive, the affected rows always missing the first record in the target table.
When run the same code on the web hoster's server, this problem does not show.
If do not use the insert function, this problem does not show.
I have tested quite a few ways trying to figure out the problem.
One solution is to place a "dummy" query inside the function, after the insert query, e.g. select * from table, it will solve the problem.
I use lunar pages in a shared environment. The xampp version is 1.7.0 in my desktop.