php-如何在m​​ysqli的更多表中添加一列?

首先,我选择一个表以从id列读取值

$db_hand1 = mysqli_connect($server, "root", "password",$database1);
$result = mysqli_query($db_hand1, "SELECT id FROM table");

现在,我想将一列添加到另一个数据库的表中:

$db_hand2 = mysqli_connect($server, "root", "password",$database2);

while($row = mysqli_fetch_array($result)){
    $user_id= $row['id'];
    $result = mysqli_query($db_hand2,"ALTER TABLE $user_id ADD us_id INT( 1 ) NULL DEFAULT '1'");
}

但是,这虽然不起作用.它总是在第一张表中添加一列.当我手动进行操作时:

$db_hand2 = mysqli_connect($server, "root", "password",$database2);
$user_id= "table_name";
$result = mysqli_query($db_hand2,"ALTER TABLE $user_id ADD us_id INT( 1 ) NULL DEFAULT '1'");

并手动设置$user_id作为其工作表的名称.

解决方法:

您覆盖$result,更改为

$db_hand2 = mysqli_connect($server, "root", "password",$database2);

    while($row = mysqli_fetch_array($result)){
        $user_id= $row['id'];
        $result_alter = mysqli_query($db_hand2,"ALTER TABLE $user_id ADD us_id INT( 1 ) NULL DEFAULT '1'");
    }
上一篇:PHP mysqli_num_rows MySQLi 函数


下一篇:php中的json对象无法读取