Home > Article > Backend Development > php change field case
When developing PHP applications, sometimes it is necessary to modify the case of fields in database tables, such as changing a field name from lower case to upper case. This article will explain how to use PHP to accomplish this task.
First, we need to connect to the MySQL database. In PHP, you can use the function mysqli_connect() to connect to the database. The following is a sample code:
$host = "localhost"; // 数据库主机名 $username = "username"; // 数据库用户名 $password = "password"; // 数据库密码 $dbname = "database"; // 数据库名 // 创建连接 $conn = mysqli_connect($host, $username, $password, $dbname); // 检查连接 if (!$conn) { die("连接失败:" . mysqli_connect_error()); }
Then you need to obtain the field information of the table. You can use the DESC command to get the field list of a table. The following is the sample code:
// 获取该表的字段信息 $sql = "DESC table_name"; $result = mysqli_query($conn, $sql); // 遍历结果集 while ($row = mysqli_fetch_assoc($result)) { $field = $row['Field']; // TODO: 修改字段大小写 } // 释放结果集 mysqli_free_result($result);
In the TODO part above, we need to change the case of the fields. You can use the ALTER TABLE command to accomplish this task. The specific operations are as follows:
Delete the field
// 删除该字段 $sql = "ALTER TABLE table_name DROP COLUMN old_field"; mysqli_query($conn, $sql);
Add a new field
// 添加新字段 $sql = "ALTER TABLE table_name ADD COLUMN new_field data_type"; mysqli_query($conn, $sql);
Change the data Copy from old field to new field
// 复制数据到新字段 $sql = "UPDATE table_name SET new_field = old_field"; mysqli_query($conn, $sql);
Delete old field
// 删除旧字段 $sql = "ALTER TABLE table_name DROP COLUMN old_field"; mysqli_query($conn, $sql);
The complete code is as follows:
$host = "localhost"; // 数据库主机名 $username = "username"; // 数据库用户名 $password = "password"; // 数据库密码 $dbname = "database"; // 数据库名 // 创建连接 $conn = mysqli_connect($host, $username, $password, $dbname); // 检查连接 if (!$conn) { die("连接失败:" . mysqli_connect_error()); } // 获取该表的字段信息 $sql = "DESC table_name"; $result = mysqli_query($conn, $sql); // 遍历结果集 while ($row = mysqli_fetch_assoc($result)) { $old_field = $row['Field']; $new_field = strtoupper($old_field); // 将该字段名转换为大写 $data_type = $row['Type']; // 删除该字段 $sql = "ALTER TABLE table_name DROP COLUMN $old_field"; mysqli_query($conn, $sql); // 添加新字段 $sql = "ALTER TABLE table_name ADD COLUMN $new_field $data_type"; mysqli_query($conn, $sql); // 复制数据到新字段 $sql = "UPDATE table_name SET $new_field = $old_field"; mysqli_query($conn, $sql); // 删除旧字段 $sql = "ALTER TABLE table_name DROP COLUMN $old_field"; mysqli_query($conn, $sql); } // 释放结果集 mysqli_free_result($result); // 关闭连接 mysqli_close($conn);
Run the above code After that, all field names in the table will be changed to uppercase. Note that this method may cause data loss or damage, please back up the database before use.
The above is the detailed content of php change field case. For more information, please follow other related articles on the PHP Chinese website!