If you find yourself writing something like the following code, try to stop and think for a while (the example uses PHP Database Objects extension):
$insert = $dbh->prepare('INSERT INTO cheapBooks VALUES (?)');
foreach($dbh->query('SELECT ID FROM Books WHERE price < 10') as $book){
// Insert cheap books into a temporary table
$insert->execute(array($book['ID']));
}