|
<?php
header ( 'http-equiv="Content-Type" content="text/html; charset=utf-8"' );
$DB_host = "localhost"; //資料庫主機
$DB_user = "root"; //資料庫使用者
$DB_psw = "root3306"; //資料庫密碼
$DB_datebase = "gk_yue39_com"; //資料庫名
$DB_charset = "utf8"; //資料庫字元集
$dbprefix="yue392_com_";
$new_dbprefix="yue39_com_";
$db = new mysqli ( $DB_host, $DB_user, $DB_psw ); //執行個體化對象
//檢查串連
if (mysqli_connect_errno ()) {
printf ( "Connect failed: %sn", mysqli_connect_error () );
exit ();
}
$db->select_db ( $DB_datebase ); //選擇操作資料庫
$db->set_charset ( $DB_charset ); //設定資料庫字元集
//執行一個查詢
$sql = 'show tables';
$result = $db->query ( $sql );
echo $result->num_rows . ' 行結果 ' . $result->field_count . ' 列內容<br/>';
//$result->data_seek('5');//從結果集中第5條開始取結果
echo '<table border="1" cellspacing="0" cellpadding="0" align="center" width="90%">';
//迴圈輸出欄位名
//$result->field_seek(2);//從欄位集中第2條開始取結果
while ( true == ($field = $result->fetch_field ()) ) {
echo '<th>' . $result->current_field . '_' . $field->name . '(' . $field->length . ')</th>';
}
//迴圈輸出查詢結果
while ( true == ($row = $result->fetch_assoc ()) ) {
echo '<tr>';
foreach ( $row as $col ) {
$sql="rename table `".$col."` to `".str_replace ( $dbprefix, $new_dbprefix, $col)."`";
if($db->query ( $sql )){
echo '<td align="center">' . $sql. '</td><td><font color="blue"> success</font></td>';
}else{
echo '<td align="center">' . $sql. '</td><td><font color="red"> failed</font></td>';
}
}
echo '</tr>';
}
echo '</table>';
$result->free ();//釋放結果集
$db->close (); //關閉串連
?> |