Php and mysql copy record from one table to another

I would like to archive the student by moving the record from one table to another. This is the code I'm 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; } 

The problem is that I get the error:

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

Is there any other way to do this, except to have a column called an archive, and vary from 0 to 1? This is because I have 30-50 pages that select table entries. :)

+6
source share
4 answers
 INSERT INTO archive_table SELECT * FROM original_table WHERE id = 1 

simply.

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

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

It is best to move one row of the table from any table to the desired table.

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

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

But,

 INSERT INTO archive TABLE SELECT FROM original_table WHERE id = 1 

- the best way to do;)

+2
source
  <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> 
0
source

Source: https://habr.com/ru/post/910374/


All Articles