码迷,mamicode.com
首页 > 数据库 > 详细

批量修改mysql数据库表前缀。

时间:2015-09-17 08:48:44      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

<!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>无标题文档</title>

 

</head>

<body>
<h2>分页查询<h2/>
<form action="fenye.php" method="get">
<table border="1" bordercolor="#FF0000">
<?php
class page
{
private $pagesize;
public $absolutepage;
private $pagecount;
private $totaINum;
private $prevpage;
private $nextpage;

function page($pagesize,$absolutepage)
{
$this->pagesize=$pagesize;
$this->absolutepage=$absolutepage;
}

public function listinfo()
{
$con=mysql_connect("localhost","root","120911");
if(!$con)
{
die(‘连接失败:‘.mysql_error);
}
if(!mysql_select_db("mydb",$con))
{
die(‘选择数据库失败:‘.mysql_error);
}
$result=mysql_query("select*from Persons");
mysql_close($con);
//所查询表中的总条数
$this->totaINum=mysql_num_rows($result);
//计算总页数
$this->pagecount=(int)(($this->totaINum-1)/($this->pagesize))+1;
//is_numeric检测变量是否为数字.absoulutepage为当前页


if($this->absolutepage==""||!is_numeric($this->absolutepage))
{
$this->absolutepage=1;
}
if($this->absolutepage>$this->pagecount)
{
$this->absolutepage=$this->pagecount;
}
if($this->totaINum>1&&$this->absolutepage>1)
{
$this->prevpage=$this->absolutepage-1;
}
if($this->absolutepage>=1&&$this->absolutepage<$this->pagecount)
{
$this->nextpage=$this->absolutepage+1;
}
?>
<tr align="center">
<td>ID</td>
<td>姓</td>
<td>名</td>
<td>年龄</td>
</tr>
<?php
if(mysql_data_seek($result,($this->absolutepage-1)*$this->pagesize))
{
for($i=0;$i<$this->pagesize;$i++)
{
if($info=mysql_fetch_array($result))
{
?>
<tr align="center">
<td><?php echo $info["personID"];?></td>
<td><?php echo $info["FirstName"];?></td>
<td><?php echo $info["LastName"];?></td>
<td><?php echo $info["Age"];?></td>
</tr>
<?php

}
}
}
}
public function toPage()
{
?>
<tr>
<td colspan="5">

一共<?php echo $this->totaINum ?>个学生,第<?php echo $this->absolutepage?>页/共
<?php echo $this->pagecount?>页:

<?php
echo "<a href=‘?absolutepage=1‘>首页</a>";
echo "<a href=‘?absolutepage=$this->prevpage‘>上一页</a>";
echo "<a href=‘?absolutepage=$this->nextpage‘>下一页</a>";
echo "<a href=‘?absolutepage=$this->pagecount‘>末页</a>";
?>

</td>
</tr>
<?php
}
}
//$obj=new page(3,$_GET[absolutepage]);
$obj=new page(3,$_GET[‘absolutepage‘]);
$obj->listinfo();
$obj->toPage();
?>
</table>
</form>

 

/*
Undefined index: 参数未传过来<br>
Use of undefined constant:书写不规范<br>
mysql_list_tables 这种写法已过时,最新写法<br>

function list_tables($database)<br>
{<br>
$rs = mysql_query("SHOW TABLES FROM $database");<br>
$tables = array();<br>
while ($row = mysql_fetch_row($rs)) {<br>
$tables[] = $row[0];<br>
}
mysql_free_result($rs);<br>
return $tables;<br>
}<br>

*/

 

 

 


<?php
$con=mysql_connect("localhost","root","120911");
if(!$con)
{
die("连接失败:".mysql_error());
}
else
{
echo "连接成功!";
}
$result=mysql_query("SHOW TABLES FROM shopdata");
while($row=mysql_fetch_row($result))
{
$data[] = $row[0];
echo $row[0],"<br>";
}
?>
<h2>批量修改mysql数据库表前缀</h2>
<?php

$dbserver=‘localhost‘;
$dbname=‘shopdata‘;//数据库名
$dbuser=‘root‘;//数据库用户名
$dbpassword=‘120911‘;//数据库密码
$old_prefix=‘biao_‘;//数据库的前缀
$new_prefix=‘ecs_‘;//数据库的前缀修改为

function listtable($dbname)
{
$rs = mysql_query("SHOW TABLES FROM shopdata");
return $rs;
}

 

if (!is_string($dbname) || !is_string($old_prefix)|| !is_string($new_prefix))
{
return false;
}
if (!mysql_connect($dbserver,
$dbuser, $dbpassword))
{
print ‘Could not connect to mysql‘;
exit;
}
//取得数据库内所有的表名
$result =listtable(‘shopdata‘);
if (!$result)
{
print "DB Error, could not list tables\n"
;
print ‘MySQL Error: ‘ . mysql_error();
exit;
}
//把表名存进$data
while ($row = mysql_fetch_row($result))
{
$data[] = $row[0];
}
//过滤要修改前缀的表名
foreach($data as $k => $v)
{
$preg = preg_match("/^($old_prefix{1})([a-zA-Z0-9_-]+)/i", //正则表达式匹配
$v, $v1);
if($preg)
{
$tab_name[$k] =$v1[2];

str_replace($old_prefix, ‘‘, $v);
}
}


if($preg)
{
//批量重命名
foreach($tab_name as $k => $v)
{
$sql = "RENAME TABLE //修改表名字符串

‘".$old_prefix.$v."‘ TO ‘".$new_prefix.$v."‘";
mysql_query($sql);
}
print 数据表前缀:.$old_prefix."<br>".已经修改为:.$new_prefix."<br>";
}

else
{
echo "您的数据库表的前缀".$old_prefix."输入错误。请检查相关的数据库表的前缀";

}

?>

</body>
</html>

批量修改mysql数据库表前缀。

标签:

原文地址:http://www.cnblogs.com/275147378abc/p/4815224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!