在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 . ' 列內容<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>';//開源代碼Vevb.com
- }
- }
- echo '</tr>';
- }
- echo '</table>';
- $result->free ();//釋放結果集
- $db->close (); //關閉連接
- ?>
新聞熱點
疑難解答