Hi Guys!
I need to do a PHP script that moves data between to tables. When Table1 has more than 10 rows, php moves those extra rows to another table, deleting them from its source.
Something like this:
while (rows on table1 > 10 ){
copy row to table2;
delete row;
}
Can anyone help me please? Thanks
5 replies to this topic
#1
Posted 08 February 2011 - 01:31 PM
|
|
|
#2
Posted 08 February 2011 - 01:53 PM
Well you'd need to either use a trigger or run a bit of code often. Note this is untested.
<?PHP
// connect here
// You will need to edit the query to match the correct criteria since you didn't provide any details I made them up
$results = mysql_query("SELECT id, name, email FROM users WHERE id > 10");
while($row=mysql_fetch_assoc($results)) {
mysql_query("INSERT INTO usersBackup (name, email) VALUES ('{$row['name']}','{$row['email']}')");
mysql_query("DELETE FROM users WHERE id={$row['id']}");
}
#3
Posted 08 February 2011 - 01:56 PM
BlaineSch said:
Well you'd need to either use a trigger or run a bit of code often. Note this is untested.
<?PHP
// connect here
// You will need to edit the query to match the correct criteria since you didn't provide any details I made them up
$results = mysql_query("SELECT id, name, email FROM users WHERE id > 10");
while($row=mysql_fetch_assoc($results)) {
mysql_query("INSERT INTO usersBackup (name, email) VALUES ('{$row['name']}','{$row['email']}')");
mysql_query("DELETE FROM users WHERE id={$row['id']}");
}This does not work. The ID is incremental...
#4
Posted 08 February 2011 - 02:16 PM
I mentioned you'd need to change it a bit depending on how you had your data. If 'id' is all you have to go by, I'd select them all and don't do it for the first 10th items.
<?PHP
// connect here
// You will need to edit the query to match the correct criteria since you didn't provide any details I made them up
$results = mysql_query("SELECT id, name, email FROM users ORDER BY id DESC");
$i = 0;
while($row=mysql_fetch_assoc($results)) {
if($i >= 10) {
mysql_query("INSERT INTO usersBackup (name, email) VALUES ('{$row['name']}','{$row['email']}')");
mysql_query("DELETE FROM users WHERE id={$row['id']}");
}
$i++;
}
#5
Posted 08 February 2011 - 11:40 PM
If extensive PHP use is not required you can do it in two queries. One will copy all the data to the other table using the INSERT ... SELECT MySQL statement. Then simply truncate, or drop the old table if it isn't used.
#6
Posted 09 February 2011 - 05:14 AM
nop...I only want to delete the rows that exceeds my condition ( more than 10)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top










