2016-07-15 198 views
2

我正在尝试使脚本将我的编码从utf8mb4更改为utf8。将utf8mb4更改为utf8

我的PHP knowlege有点过时,我不能让脚本与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

比k你的帮助! :)

+0

mysqli_query()需要第一个参数作为您的连接。用它作为'$ result = mysqli_query($ mysqli,'show tables'); '阅读http://php.net/manual/en/mysqli.query.php – Saty

+0

我试过这个,但现在我得到50次同样的错误:'mysqli_query()期望至少有2个参数,1给出' – Relisora

+0

你有没有在foreach循环查询里面改变? – Saty

回答

1

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"); 
    } 
} 
相关问题