Check if ID exist or not Here I have a class where I'm checking if model_id exists in the table models_web_tbl.

Everything works like a charm but I just want to know if I'm on the right path.

class test extends dbh{

public function modelExist($model_id) {

$sql = "SELECT * FROM models_web_tbl WHERE model_id = ?";

    $stmt = $this-&gt;connect()-&gt;prepare($sql);
    $stmt-&gt;execute([$model_id]);
    $match = $stmt-&gt;fetch(PDO::FETCH_OBJ);

        if ($match &gt; 0){

            echo "found";

        }else {

            $model_web_twitter = "twitter";

            $sql = "INSERT INTO models_web_tbl (model_web_twitter, model_id) VALUES (?, ?)";
            $stmt = $this-&gt;connect()-&gt;prepare($sql);
            $stmt-&gt;execute([$model_web_twitter, $model_id]);

            }

}
}
$model_id = 66;
$exist = new test ();
$exist = $exist-&gt;modelExist($model_id);
Function modelExist should check if model exists and return boolean. Your function does something completely different. It only needs that check for its job.

public function modelExists(int $modelId): bool;


To check existence of a row, you don't need all its columns. Select just 1 (literally) and check if you have received a row from the query or not.

SELECT 1 FROM models_web_tbl WHERE model_id = ?


Create another function for the insert

public function insertModel(int $modelId, string $modelWeb): void


Maybe you wanted to combine it into another function.

public function tryInsert(int $modelId, string $modelWeb): bool
{
  if ($this-&gt;modelExists($modelId)) {
    return false;
  }
  $this-&gt;insert($modelId, $modelWeb);
  return true;
}


Don't connect to database again for every query

class test
{
  private PDO $connection;
  function __construct(PDO $connection) {
    $this-&gt;connection = $connection;
  }
  function modelExists(int $modelId): bool
  {
    // ...
    $this-&gt;connection-&gt;prepare($sql);
    // ...
  }

  function insert(int $modelId, $modelWeb): void
  {    
    // ...
    $this-&gt;connection-&gt;prepare($sql);
    // ...
  }
}


$connection = connect();
$test = new test($connection);
  // I don't know why it was important to assign 'twitter' to a variable first :)
if (!$test-&gt;tryInsert($model_id, 'twitter')) {
  // echo definitely dont belong into exists-check function
  echo "found";
}