Creating efficient PDO functions I have the following functions:

 function getExample($id, $otherid) {
            global $db;
            $query = 'SELECT * FROM examples WHERE id = :id AND otherid= :otherid';
            try { 
                $statement = $db-&gt;prepare($query);
                $statement-&gt;bindValue(':id', $id);
                $statement-&gt;bindValue(':otherid', $otherid);
                $statement-&gt;execute();
                $result = $statement-&gt;fetchAll();
                $statement-&gt;closeCursor();
                return $result;
            } catch (PDOException $e) {
                $error_message = $e-&gt;getMessage();
                display_db_error($error_message);
            }
        }

 function getSecExample($id) {
            global $db;
            $query = 'SELECT * FROM examples WHERE id= :id';
            try { 
                $statement = $db-&gt;prepare($query);
                $statement-&gt;bindValue(':id', $id);
                $statement-&gt;execute();
                $result = $statement-&gt;fetchAll();
                $statement-&gt;closeCursor();
                return $result;
            } catch (PDOException $e) {
                $error_message = $e-&gt;getMessage();
                display_db_error($error_message);
            }
        }


Is there a way to make this one function instead of two? I want it to be secure and done with the best practices.
I'm not sure why you think that forcing this into one function will make it a "best practice," and that doesn't seem right anyways.

I suppose one way you could do it is:

 WHERE id = :id AND otherid LIKE :otherid


If it were to be in the second function, :otherid would be a wildcard (%). Otherwise just have your value. Check out this.