Home > Backend Development > PHP Problem > How to set table prefix in php

How to set table prefix in php

藏色散人
Release: 2023-03-04 18:16:01
Original
2885 people have browsed it

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.

How to set table prefix in php

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 =&#39;pre_&#39;;                     //替换后的前缀
        $seach = &#39;pre1_&#39;;                     //要替换的前缀
        $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[&#39;0&#39;]);
                
                mysql_query("rename table $name[0] to $table");
        }
 
?>
Copy after login

If you add a prefix, you only need to change it a little

$table = str_replace($seach,$replace,$name[&#39;0&#39;]);换成
   $table = $replace.$name[&#39;0&#39;];
Copy after login

.

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template