php 和 mysql 将记录从一张表复制到另一张表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9640881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:20:30  来源:igfitidea点击:

php and mysql copy record from one table to another

phpmysqlcopy

提问by David Passmore

I would like to archive a student by moving the record from one table to another. This is the code I am trying to use:

我想通过将记录从一张桌子移到另一张桌子来存档学生。这是我尝试使用的代码:

<?php
ini_set('memory_limit', '100M');
$sql="Select * from `register` where student_id=".$student_id;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

//Call the function to archive the table
//Function definition is given below
archive_record(archive,$row);

//Once you archive, delete the record from original table

$sql = "Delete from `register` where student_id=".$student_id;
mysql_query($sql);


function archive_record($archived_tablename,$row)
{
    $sql = "insert into $archived_tablename values(";
    $i=0;
    while($i<(count($row)-1))
    {
        $sql.="'".$row[$i]."',";
    }
    $i=$i+1;

    $sql.="'".$row[$i]."'";
    $sql.=")";

    mysql_query($sql);
    return true;
}

Problem I am having is that i am getting error:

我遇到的问题是我收到错误:

Fatal error: Out of memory (allocated 80478208) (tried to allocate 80216043 bytes) in /archive-student.php on line XX

致命错误:第 XX 行 /archive-student.php 中的内存不足(已分配 80478208)(试图分配 80216043 字节)

Is there any different way to do this, except for have a column called archive and changing from 0 to 1? This is because I have 30-50 pages selecting the table's records. :)

除了有一个名为存档的列并从 0 更改为 1 之外,还有什么不同的方法可以做到这一点?这是因为我有 30-50 页选择表的记录。:)

回答by dev-null-dweller

INSERT INTO archive_table
SELECT * FROM original_table WHERE id = 1

simple as that.

就那么简单。

If tables have different column number, other layout etc., you will have to specify columns :too

如果表格具有不同的列号、其他布局等,则必须指定列:太

INSERT INTO archive_table(field1, field2, field3)
SELECT field7, field8, field9 FROM original_table WHERE id = 1

回答by shaz3e

INSERT INTO new_table SELECT id FROM old_table

This is best practice to move one table row from any table to required table.

这是将表行从任何表移动到所需表的最佳做法。

回答by Xavinou

while($i<(count($row)-1))
    {
        $sql.="'".$row[$i]."',";
    }
    $i=$i+1;

You have to do $i=$i+1;inside the loop...

你必须$i=$i+1;在循环内做...

But,

但,

INSERT INTO archive TABLE
SELECT FROM original_table WHERE id = 1

is the best way to do ;)

是最好的方法;)

回答by Raghav

    <html>
    <body bgcolor="lightblue">
    <h1>Data fetched from register.php</h1>
    <table  border=1 cellspacing=0 cellpadding=10px>
    <tr>
    <th>id</th>
    <th>fullname</th>
    <th>email</th>
    <th>username</th>
    <th>dob</th>
    <th>password</th>
    <th>gender</th>
    <th>lanuage</th>
    <th>country</th>
    </tr>
    <?php
    $xyz=mysqli_connect("localhost", "root", "", "myprogrammes");
    $abc=mysqli_query($xyz, "select * from table");
    while ($bb=mysqli_fetch_array($abc)){
          $id1=$bb['id'];
          $fullname1=$bb['fullname'];
          $email1=$bb['email'];
          $username1=$bb['username'];
          $dob1=$bb['dob'];
          $password1=$bb['password'];
          $gender1=$bb['gender'];
          $lanuage1=$bb['lanuage'];
          $country1=$bb['country'];
    ?>
    <tr>
    <th><?php echo $id1 ; ?></th>
    <th><?php echo $fullname1 ; ?></th>
    <th><?php echo $email1 ; ?></th>
    <th><?php echo $username1 ; ?></th>
    <th><?php echo $dob1 ; ?></th>
    <th><?php echo $password1 ;  ?></th>
    <th><?php echo $gender1 ; ?></th>
    <th><?php echo $lanuage1 ; ?></th>
    <th><?php echo $country1 ; ?></th>
    </tr>
<?php  } ?>
    </body>
    </html>