Monday, April 27, 2009

php code template

<?php
// ### Add by Danny Hsieh. 2009-03-07 15:06:27.
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

ini_set('max_execution_time', 0);

include('./../common.php');

echo '[START] ' . date('Y-m-d h:i:s') . "\n";

$conn = new mysqli($db_server, $db_user, $db_pass);

/* check connection */
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}

/* change db to your db */
$conn->select_db($db_name['danny_testing']);

mb_internal_encoding("UTF-8");
$conn->query("SET NAMES 'utf8'");
$conn->query("SET CHARACTER SET 'utf8'");

### SET AUTOCOMMIT to 0 (for speeding up the process)
$conn->query("SET autocommit=0;");

$sql = "SELECT ";
$sql .= " ";
$sql .= " ";
$rs = $conn->query($sql);
if ($conn->error) { die('Invalid query: ' . $conn->error); }

if ($rs) {

$rowCount = 0;
while ($row = $rs->fetch_array()) {

### COMMIT (save) the data every 5000 rows
if ($rowCount % 5000 == 0) {
$conn->query('COMMIT;');
}

$rowCount++;
}
}

### COMMIT (save) the rest of rows.
$conn->query('COMMIT;');

$rs->close(); // 另一種是 $conn->free_result

$rowData = array(
'field_name' => $row['field_name'],
);

// $conn->query(sql_rewrite_insert('the_table_name', $rowData));
// echo sql_rewrite_insert('the_table_name', $rowData) . '
';
// if ($conn->error) { die('Invalid query: ' . $conn->error); }
// if ($conn->error) { printf("Invalid query: %s\n", $conn->error) . '
'; continue; };
//
// $whereFieldArr = array(
// 'id' => $id_list[$key],
// );
// $conn->query(sql_rewrite_update('the_table_name', $rowData, $whereFieldArr));
// echo sql_rewrite_update('the_table_name', $rowData, $whereFieldArr) . '
';
// if ($conn->error) { die('Invalid query: ' . $conn->error); }
// if ($conn->error) { printf("Invalid query: %s\n", $conn->error) . '
'; continue; };

$conn->close();



echo '[END] ' . date('Y-m-d h:i:s') . "\n";

echo 'Done!';
?>

No comments: