php – Check if there is an ID or not


Function modelExist You must check if the model exists and return Boolean. Its function does something completely different. You just need that to check your work.

public function modelExists(int $modelId): bool;

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

SELECT 1 FROM models_web_tbl WHERE model_id = ?

Create another function for insertion

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

Do not reconnect to the database for each query

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

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

Maybe you wanted to combine it in another function, but I guess it's some kind of test.

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