繁体   English   中英

PHP和mysql将记录从一个表复制到另一个表

[英]php and mysql copy record from one table to another

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

<?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;
}

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

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

除了有一个名为archive的列并从0更改为1之外,是否有其他方法可以实现? 这是因为我有30-50页选择表的记录。 :)

INSERT INTO archive_table
SELECT * FROM original_table WHERE id = 1

就那么简单。

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

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

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

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

您必须执行$i=$i+1; 在循环中...

但,

INSERT INTO archive TABLE
SELECT FROM original_table WHERE id = 1

是最好的方法;)

    <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM