I have a PHP bot running on a shared host. My account often get suspended. When I asked to web hosting service, they said that my account suspended because of excessive MySQL usage.There is an upload.php and db_functions.php files. Upload PHP runs every half hour. I open and close MySQL connections in every function.Is building a MySQL connection at the beginning of the upload.php and closing at the end of the upload.php able to prevent excessive usage?Which one is effective between these two situation?1st situationupload.phprequire_once(WEBSITE_ROOT.'/'.APP_DIRECTORY.'/functions/db_fns.php');$subreddits = subreddit_getir($db_user[category]);$posts = grab_reddit_picture($subreddits);foreach($posts as $post){        $hashtags = make_hashtag($db_user[category], $post[subreddit]); //#city #culture etc.        if($post[type]=='resim'){            $rawImage = file_get_contents($post[url]);            if(!$rawImage){break;}            $basename = preg_replace('/^.+[\\\\\\/]/', '', $post[url]);            if(!is_picture_inserted($db_user[tw_id], $post[url])){                $pic_id = insert_picture($db_user[tw_id], $post[title], $post[url], $post[type]);                file_put_contents(WEBSITE_ROOT.'/'.APP_DIRECTORY.'/images/'.$basename, $rawImage);                //image_text($basename, $db_user[tw_name]);                // assign access token on each page load                $cb->setToken($db_user['oauth_token'], $db_user['oauth_token_secret']);                $reply = $cb->statuses_updateWithMedia(array(                    'status'  => substr(convert_hashtag($post[title]), 0, 72).' '.$hashtags.' ',                    'media[]' => WEBSITE_ROOT.'/'.APP_DIRECTORY.'/images/'.$basename                ));                print_r($reply);                //break;            }        }else{            if(!is_picture_inserted($db_user[tw_id], $post[url])){                insert_picture($db_user[tw_id], $post[title], $post[url], $post[type]);                $cb->setToken($db_user['oauth_token'], $db_user['oauth_token_secret']);                $reply = $cb->statuses_update(array(                    'status'  => substr(convert_hashtag($post[title]), 0, 100).' '.$post['url'].' '.$hashtags                ));                print_r($reply);                    }        }}db_functions.php<?php    function db_connect() {        $connection = new mysqli(MYSQL_HOSTNAME, USERNAME, PASSWORD, DATABASE);        if (!$connection)        {          echo 'No connection!';          mysql_error();         }        if (!$connection->select_db(DATABASE))        {          echo 'No database!';           mysql_error();        }        $connection->query(SET NAMES UTF8);        return $connection;        }    function db_result_to_array($result)     {        $res_array = array();        for ($count = 0; $row = mysqli_fetch_assoc($result); $count++)        {            $res_array[$count] = $row;            }        return $res_array;    }    function is_user_created($id, $tablo_adi)    {        $conn = db_connect();        $query = SELECT id FROM .$tablo_adi. WHERE tw_id = .$id;        $result = $conn->query($query);        $result = $result->fetch_array();        if($result)        {            return $result;        }        else        {            return false;        }        if(mysqli_ping($conn))        {            $conn->close();         }    }    function create_user($user_id, $oauth_token, $oauth_token_secret){        $conn = db_connect();            $query = sprintf(INSERT into reddit_twitter SET                 tw_id = %s,                oauth_token = '%s',                oauth_token_secret = '%s',                $user_id,                $oauth_token,                $oauth_token_secret);        $result = $conn->query($query);        if(!$result){            echo 'Dnyay ele geirmeye falan m alyorsun?';            echo $conn->error;        }else{            return $conn->insert_id;        }        $conn->close();    }    function select_users(){        $conn = db_connect();        $query = SELECT * FROM reddit_twitter ORDER by id DESC;        $result = $conn->query($query);        $result = db_result_to_array($result);        return $result;        $conn->close();    }    function insert_picture($tw_id, $title, $picture, $type){        $conn = db_connect();        $query = sprintf(INSERT into pics SET             tw_id = '%s',            title = '%s',            picture = '%s',            type = '%s'            ,            $tw_id,            $conn->real_escape_string($title),            $picture,            $type            );        $result = $conn->query($query);        if(!$result){            echo 'Dnyay ele geirmeye falan m alyorsun?';            echo $conn->error;        }else{            return $conn->insert_id;        }        $conn->close();    }    function is_picture_inserted($uid, $basename)    {        $conn = db_connect();        $query = SELECT id FROM pics WHERE tw_id = .$uid. AND picture = '.$basename.';        $result = $conn->query($query);        $result = mysqli_fetch_assoc($result);        if($result)        {            return $result[id];        }        else        {            echo $conn->error;            return false;        }        if(mysqli_ping($conn))        {            $conn->close();         }    }?>2nd situationupload.phprequire_once(WEBSITE_ROOT.'/'.APP_DIRECTORY.'/functions/db_fns.php');$conn = db_connect();$subreddits = subreddit_getir($db_user[category]);$posts = grab_reddit_picture($subreddits);foreach($posts as $post){        $hashtags = make_hashtag($db_user[category], $post[subreddit]); //#city #culture etc.        if($post[type]=='resim'){            $rawImage = file_get_contents($post[url]);            if(!$rawImage){break;}            $basename = preg_replace('/^.+[\\\\\\/]/', '', $post[url]);            if(!is_picture_inserted($db_user[tw_id], $post[url])){                $pic_id = insert_picture($db_user[tw_id], $post[title], $post[url], $post[type]);                file_put_contents(WEBSITE_ROOT.'/'.APP_DIRECTORY.'/images/'.$basename, $rawImage);                //image_text($basename, $db_user[tw_name]);                // assign access token on each page load                $cb->setToken($db_user['oauth_token'], $db_user['oauth_token_secret']);                $reply = $cb->statuses_updateWithMedia(array(                    'status'  => substr(convert_hashtag($post[title]), 0, 72).' '.$hashtags.' ',                    'media[]' => WEBSITE_ROOT.'/'.APP_DIRECTORY.'/images/'.$basename                ));                print_r($reply);                //break;            }        }else{            if(!is_picture_inserted($db_user[tw_id], $post[url])){                insert_picture($db_user[tw_id], $post[title], $post[url], $post[type]);                $cb->setToken($db_user['oauth_token'], $db_user['oauth_token_secret']);                $reply = $cb->statuses_update(array(                    'status'  => substr(convert_hashtag($post[title]), 0, 100).' '.$post['url'].' '.$hashtags                ));                print_r($reply);                    }        }}$conn->close(); db_functions.php<?php    function db_connect() {        $connection = new mysqli(MYSQL_HOSTNAME, USERNAME, PASSWORD, DATABASE);        if (!$connection)        {          echo 'No connection!';          mysql_error();         }        if (!$connection->select_db(DATABASE))        {          echo 'No database!';           mysql_error();        }        $connection->query(SET NAMES UTF8);        return $connection;        }    function db_result_to_array($result)     {        $res_array = array();        for ($count = 0; $row = mysqli_fetch_assoc($result); $count++)        {            $res_array[$count] = $row;            }        return $res_array;    }    function is_user_created($id, $tablo_adi)    {        $query = SELECT id FROM .$tablo_adi. WHERE tw_id = .$id;        $result = $conn->query($query);        $result = $result->fetch_array();        if($result)        {            return $result;        }        else        {            return false;        }    }    function create_user($user_id, $oauth_token, $oauth_token_secret){            $query = sprintf(INSERT into reddit_twitter SET                 tw_id = %s,                oauth_token = '%s',                oauth_token_secret = '%s',                $user_id,                $oauth_token,                $oauth_token_secret);        $result = $conn->query($query);        if(!$result){            echo 'Dnyay ele geirmeye falan m alyorsun?';            echo $conn->error;        }else{            return $conn->insert_id;        }    }    function select_users(){        $query = SELECT * FROM reddit_twitter ORDER by id DESC;        $result = $conn->query($query);        $result = db_result_to_array($result);        return $result;    }    function insert_picture($tw_id, $title, $picture, $type){        $query = sprintf(INSERT into pics SET             tw_id = '%s',            title = '%s',            picture = '%s',            type = '%s'            ,            $tw_id,            $conn->real_escape_string($title),            $picture,            $type            );        $result = $conn->query($query);        if(!$result){            echo 'Dnyay ele geirmeye falan m alyorsun?';            echo $conn->error;        }else{            return $conn->insert_id;        }    }    function is_picture_inserted($uid, $basename)    {        $query = SELECT id FROM pics WHERE tw_id = .$uid. AND picture = '.$basename.';        $result = $conn->query($query);        $result = mysqli_fetch_assoc($result);        if($result)        {            return $result[id];        }        else        {            echo $conn->error;            return false;        }    }?>
Upload bot, using MySQL excessively
performance;php;mysql;comparative review