在php中有時我們要替換資料庫中表首碼但是又不苦於一個個表去修改首碼吧,下面我自己寫了一個mysqli批量替換資料庫表首碼的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 '
//迴圈輸出查詢結果 while ( true == ($row = $result->fetch_assoc ()) ) { echo '
echo '
';
| ' . $result->current_field . '_' . $field->name . '(' . $field->length . ') | ';}
'; 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 '
';}
'; $result->free ();//釋放結果集 $db->close (); //關閉串連 ?> |
http://www.bkjia.com/PHPjc/632925.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632925.htmlTechArticle在php中有時我們要替換資料庫中表首碼但是又不苦於一個個表去修改首碼吧,下面我自己寫了一個mysqli批量替換資料庫表首碼的php程式,希...