如何在mysqli中的更多表中添加一列?

时间:2014-01-20 08:08:37

标签: php mysqli

首先,我选择一个表来读取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'");
}

然而,不起作用。它总是在第一个表中添加一列。当我这样做时,manualy:

$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 以获取其工作的表格名称。

1 个答案:

答案 0 :(得分:3)

您覆盖$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'");
    }