How to set the table prefix in php: first enter the database user name and password; then connect to the database; then modify the table prefix through the "str_replace($seach,$replace,$name['0']);" statement. Can.
Recommended: "PHP Video Tutorial"
php batch change database table prefix
We often encounter the problem of replacing or adding database table prefixes. Exporting the database, making batch changes on Notepad, and then exporting is also a method, but it is not convenient enough.
It can be easily done through the following method. The code is as follows, if you find it useful, please give me some advice.
<?php $database = "databaseName"; //数据库名称 $user = "root"; //数据库用户名 $pwd = "pwd"; //数据库密码 $replace ='pre_'; //替换后的前缀 $seach = 'pre1_'; //要替换的前缀 $db=mysql_connect("localhost","$user","$pwd") or die("连接数据库失败:".mysql_error()); //连接数据库 $tables = mysql_list_tables("$database"); while($name = mysql_fetch_array($tables)) { $table = str_replace($seach,$replace,$name['0']); mysql_query("rename table $name[0] to $table"); } ?>
If you add a prefix, you only need to change it a little
$table = str_replace($seach,$replace,$name['0']);换成 $table = $replace.$name['0'];
.
The above is the detailed content of How to set table prefix in php. For more information, please follow other related articles on the PHP Chinese website!