Repeat PHP code

2013 年 11 月 19 日4740

Building on other answers, here's how to do the loop and hit the database only once.

$query = "INSERT INTO downloads (downloadkey, file, expires) VALUES ";







while ($row = mysql_fetch_array($resultstwo)) {



$strKey = createKey();



$time = time()+(60*60);







$query .= "('{$strKey}', '{$row['filename']}', {$time}),";



echo "<a href='download.php?key=$strKey'>{$row['caption']}</a>";



}







$query = substr ( $query, 0, -1 ); // Remove the last comma.







mysql_query ( resDB, $query );



The end result of the query will be something like:

INSERT INTO downloads (downloadkey, file, expires) VALUES 



('adfsdfgsdgs', 'Bladerunner.avi', 12345678),



('dfghfyadfsg', 'How_I_met_your_mother.avi', 12345678),



('34t34t3t', 'Simpson_the_movie.avi', 12345678),



('taert43te', '{$row['filename']}', 12345678),



('at43t3t', '{$row['filename']}', 12345678),



This will insert all the values into mysql using this one query only.

0 0