gpt4 book ai didi

php - 将 utf8mb4 更改为 utf8

转载 作者:行者123 更新时间:2023-11-29 02:48:50 25 4
gpt4 key购买 nike

我正在尝试制作一个脚本,将我的编码从 utf8mb4 更改为 utf8。

我的 PHP 知识有点过时,我无法使脚本与 mysqli 一起工作。

这是我的基本脚本:

<?php
$con = mysql_connect('localhost','user','password');
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db('dbname');
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}}
echo "The collation of your database has been successfully changed!";
?>

它不起作用,所以我尝试将它更新到 mysqli,现在我有了这个:

<?php
$mysqli = new mysqli("localhost", "root", "", "test");

if (mysqli_connect_errno()) {
printf("connexion error : %s\n", mysqli_connect_error());
exit();
}

if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("The database is : %s.\n", $row[0]);
$result->close();
}

$result=mysqli_query('show tables');
while($tables = mysqli_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysqli_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}}
echo "The collation of your database has been successfully changed!";
?>

我认为问题出在最后一部分,我得到的错误是:

php errors

感谢您的帮助! :)

最佳答案

mysqli_query()需要第一个参数作为您的连接。用作

$result = mysqli_query($mysqli, 'show tables');
while ($tables = mysqli_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysqli_query($mysqli, "ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}
}

关于php - 将 utf8mb4 更改为 utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com