如何在mysqli中的更多表中添加一列?(how to add one column into more tables in mysqli?)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
如何在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'"); }

更多推荐

本文发布于:2023-04-17 09:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6b481ac88cc2db95361fab2c7d27fae5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更多   如何在   mysqli   tables   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!