$sql="SELECT * FROM addressbook WHERE deprecated='0000-00-00 00:00:00' AND thumbPhoto IS NOT NULL"; $recordset=mysql_query($sql); $row=mysql_fetch_assoc($recordset); while($row=mysql_fetch_assoc($recordset)){ $handle = fopen("./images/".$row['photo'],"wb"); fwrite($handle, $row['thumbPhoto']); fclose($handle);}