php+mysqli實現批量替換資料庫表首碼的方法,mysqli首碼
本文執行個體講述了php+mysqli實現批量替換資料庫表首碼的方法。分享給大家供大家參考。具體分析如下:
在php中有時我們要替換資料庫中表首碼但是又不苦於一個個表去修改首碼,這裡我自己寫了一個mysqli批量替換資料庫表首碼的php程式,感興趣的朋友可以參考一下,代碼如下:
複製代碼 代碼如下:<?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 . ' 列內容
';
//$result->data_seek('5');//從結果集中第5條開始取結果
echo '
'; //迴圈輸出欄位名 //$result->field_seek(2);//從欄位集中第2條開始取結果 while ( true == ($field = $result->fetch_field ()) ) { echo '
| ' . $result->current_field . '_' . $field->name . '(' . $field->length . ') | '; } //迴圈輸出查詢結果 while ( true == ($row = $result->fetch_assoc ()) ) { echo '
'; foreach ( $row as $col ) { $sql="rename table `".$col."` to `".str_replace ( $dbprefix, $new_dbprefix, $col)."`"; if($db->query ( $sql )){ echo '
| ' . $sql. ' |
success | '; }else{ echo '
' . $sql. ' |
failed | '; } } echo '
'; } echo '
';
$result->free ();//釋放結果集
$db->close (); //關閉串連
?>
希望本文所述對大家的php程式設計有所協助。
http://www.bkjia.com/PHPjc/934933.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/934933.htmlTechArticlephp+mysqli實現批量替換資料庫表首碼的方法,mysqli首碼 本文執行個體講述了php+mysqli實現批量替換資料庫表首碼的方法。分享給大家供大家參考。...