我有一個用於批量更新的PHP腳本,在我的數據庫中存在90,585行,但只更新了85,282行,我不知道爲什麼,這是我的腳本:有些記錄沒有用PHP和MySql批量更新
//limit the select to 100
//Bulk update is very slow, is you set a limit very high server crash
$limit = 100;
//$messages = array();
$updated_posts = 0;
//maybe there are better solutions for this
for ($i=0;$i<=$totalMsg;$i+=$limit)
{
$get_posts = mysqli_query($conn,
"SELECT id_msg, body
FROM " . $to_prefix ."messages
WHERE id_msg != 0
LIMIT " . $i ."," . $limit);
//The post Array
$messages = array();
while($row = mysqli_fetch_assoc($get_posts))
{
$messages[$row['id_msg']] = array(
'body' => fixBBCTags($row['body']),
);
}
//update data!, good luck!!
bulkUpdate($conn,$to_prefix."messages","id_msg","body",$messages);
$updated_posts += mysqli_affected_rows($conn);
}// for loop
這是bulkUpdate()函數:
function bulkUpdate($conn, $table, $id_column, $update_column, array &$idstovals)
{
global $error;
//prepare the Bulk Update SQL
$sql = "update " . $table . " set " . $update_column . " = CASE " . $id_column ;
foreach($idstovals as $id => $val)
{
$sql .= " WHEN " . "'" . $id . "'" . " THEN " . "'" . mysqli_real_escape_string($conn,$val[$update_column]) . "'" . " \n";
}
$sql .= " END
WHERE " . $id_column. " in (" . implode(',', array_keys($idstovals)) . ")";
//reset the array
//$idstovals=array();
//try update the bulk
$update = mysqli_query($conn,$sql);
if(mysqli_error($conn))
$error = mysqli_error($conn);
}
所有行必須更新,還有更好的爲這個解決方案?
問候。
這對我很有用!謝謝! – Enecumene