May
28
2013
mysql数据库表前缀批量修改工具
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>mysql数据库表前缀批量修改工具</title> <style> body { font: 12px/22px "Microsoft YaHei", SimSun; } input,select,texteare,button { font-family: "Microsoft YaHei", SimSun; } </style> </head> <body> <?php if (empty ( $_POST )) { ?> <form action="" method="post" onsubmit="return check();"> <h2>Mysql数据库表前缀批量修改工具</h2> <div> <fieldset> <legend>数据库性息</legend> <div> <table width="200" border="1" cellpadding="2" cellspacing="0"> <tr> <td>主机:</td> <td><input id="host" name="host" type="text" value="localhost" /></td> </tr> <tr> <td>用户:</td> <td><input id="user" name="user" type="text" value="root" /></td> </tr> <tr> <td>密码:</td> <td><input id="password" name="password" type="password" /></td> </tr> <tr> <td>数据库:</td> <td><input id="database" name="database" type="text" /></td> </tr> <tr> <td>旧前缀:</td> <td><input id="dbprefix" name="dbprefix" type="text" /></td> </tr> <tr> <td>新前缀:</td> <td><input id="new_dbprefix" name="new_dbprefix" type="text" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" /></td> </tr> </table> </div> </fieldset> </div> </form> <script type="text/javascript"> var host = document.getElementById("host"); //host var user = document.getElementById("user"); //user var database = document.getElementById("database"); //database var dbprefix = document.getElementById("dbprefix"); //dbprefix var new_dbprefix = document.getElementById("new_dbprefix"); //new_dbprefix function check(){ if(host.value==""){ alert('请输入Mysql主机!'); return false; } if(user.value==""){ alert('请输入Mysql用户!'); return false; } if(database.value==""){ alert('请输入操作数据库!'); return false; } if(dbprefix.value==""){ alert('请输入旧表前缀!'); return false; } if(new_dbprefix.value==""){ alert('请输入新表前缀!'); return false; } return true; } </script> <?php } else { $DB_host = trim ( @$_POST ['host'] ); // 数据库主机 $DB_user = trim ( @$_POST ['user'] ); // 数据库用户 $DB_psw = trim ( @$_POST ['password'] ); // 数据库密码 $DB_datebase = trim ( @$_POST ['database'] ); // 数据库名 $DB_charset = "utf8"; // 数据库字符集 $dbprefix = trim ( @$_POST ['dbprefix'] ); $new_dbprefix = trim ( @$_POST ['new_dbprefix'] ); $db = @ new mysqli ( $DB_host, $DB_user, $DB_psw ); // 实例化对象 // 检查连接 if (mysqli_connect_errno ()) { printf ( "Connect failed: %s\n", mysqli_connect_error () ); exit (); } $db->select_db ( $DB_datebase ); // 选择操作数据库 $db->set_charset ( $DB_charset ); // 设置数据库字符集 // 执行一个查询 $sql = "show tables;"; $result = $db->query ( $sql ); ?> <p> <?php echo $result->num_rows; ?> 行结果 <?php echo $result->field_count; ?> 列内容</p> <table border="1" cellspacing="0" cellpadding="0" align="center" width="50%"> <?php // 循环输出字段名 while ( true == ($field = $result->fetch_field ()) ) { ?> <tr><th colspan="2"><?php echo $result->current_field; ?>_<?php echo $field->name ?> (<?php echo $field->length; ?>)</th></tr> <tr><th align="center">SQL</th><th align="center">Result</th></tr> <?php } // 循环输出查询结果 while ( true == ($row = $result->fetch_assoc ()) ) { ?> <tr> <?php foreach ( $row as $col ) { $new_table = str_replace ( $dbprefix, $new_dbprefix, $col ); $sql = "rename table `{$col}` to `{$new_table}`;"; if ($db->query ( $sql )) { ?> <td align="center"><?php echo $sql;?></td> <td align="center"><font color="blue"> success</font></td> <?php } else { ?> <td align="center"><?php echo $sql;?></td> <td align="center"><font color="red"> failed</font></td> <?php } } ?> </tr> <?php } ?> </table> <?php $result->free (); // 释放结果集 $db->close (); // 关闭连接 } ?> </body> </html>
最活跃的读者