循环运行正好20次,但MySql数据库仅插入数据16次

I am developing a parser in which every time the parser will copy an image from URL to remote server, and save the details in MySql database. what i am doing is like this:

function scrap_photo($frompage, $permalink, $url, $message, $createdtime, $type, $imgid) {
    global $last_cron_timestamp;
    global $cxn;

    if ($createdtime > $last_cron_timestamp && $type == "photo") {


            $current_time_stamp = round(microtime(true) * 1000);
            $imgpath = "photo/".$current_time_stamp.".jpg";
            echo $imgpath;
            $fp = fopen($imgpath, "w");

            $handle = curl_init();
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
            curl_setopt($handle, CURLOPT_URL, $url);
            curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($handle, CURLOPT_FAILONERROR, true);
            curl_setopt($handle, CURLOPT_TIMEOUT, 300);
            curl_setopt($handle, CURLOPT_FILE, $fp);
            curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_exec($handle);
            curl_close($handle);
            fclose($fp);


            $query = "INSERT INTO images (permalink, url, message, createdtime, imgid, frompage, type, photo_name, photo_path) VALUES ('$permalink', '$url', '$message', '$createdtime', '$imgid', '$frompage', '$type', '$current_time_stamp', '$imgpath')";

            mysqli_query($cxn, $query); 
    }
}

this function 'scrap photo' is being used to save the image to directory and to write the details in DB.

The problem that i am facing is, the loop runs for 20 times, this function is being called 20 times, and even the image count in the folder is 20, but in MySql database, only 16 query is getting inserted.

I can't figure out how or why, help!

  • Make sure your condition if ($createdtime > $last_cron_timestamp && $type == "photo") { passes for those 20 photos,
  • use mysqli_error to check if database returns errors as for example your values might be null while database doesn't allow them to be null.