如何在mysqli中的更多表中添加一列?(how to add one column into more tables in 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'");并且manualy设置$ user_id作为它工作的表的名称。
First I SELECT a table to read values from id column
$db_hand1 = mysqli_connect($server, "root", "password",$database1); $result = mysqli_query($db_hand1, "SELECT id FROM table");Now I want to add one column into tables in another database:
$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'"); }However this while doesn't work. It always add a column into the first table. When I do it 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'");and manualy set $user_id for a name of table it works.
最满意答案
你重写$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'"); }you override the $result, change like
$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'"); }更多推荐
发布评论