0

I am trying to write queries in drupal 8 , I know that I must use the delete() function , however just for testing on my draft I tried the following query and it worked :

$connection = \Drupal::database();
$sql = "DELETE FROM role_expire WHERE uid = :uid AND rid = :rid ";
$query_ = $connection->query($sql, array(":uid" => $drupal_user_id ,":rid" => $role_id));  

My question is should I ADD a execute() at the end like then following example that I found while searching and if yes, then how did my query manage to work without it ? :

 $database = \Drupal::database();
 $query = $database->query("DELETE FROM table WHERE name='jane' AND nickmane='" . $nickname . "'");
 $query->execute();

same question for the INSERT query.

1 Answer 1

0

You do not need $query->execute.

Drupal gives this example. As you have already mentioned there is recommendation to not use query for simple INSERT, UPDATE or DELETE queries.

And you can see in Connection::query that the query is executed.

1
  • Thank you so much Commented Apr 14, 2021 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.