MariaDB: No database selected error on some query

I have a php code that rise a query against a MariaDB (using MariaDB 10.5.11 on debian 11) table; I use php-mysql prepared queries for this task as reported in the code below:

if($  this->dbcon->begin_transaction() === false) {         $  this->errNum = -1;         $  this->errText = "Unable to start transaction: " . $  this->dbcon->errno . " - " . $  this->dbcon->error;         return false; }  try {     $  query = file_get_contents("recursivelyRemoveShares.sql");   // (1) If replaced with a SELECT works fine!      if($  query === false) {         $  this->errNum = -1;         $  this->errText = "Unable to read query (0)";         return false;     }      $  stmt = $  this->dbcon->prepare($  query);      // Err 1046: No database selected     if($  stmt === false) {         $  this->errNum = -1;         $  this->errText = "Unable to prepare statement: " . $  this->dbcon->errno . " - " . $  this->dbcon->error;         return false;     }          $  stmt->bind_param("s", $  uuid);     $  stmt->execute();      // Commit transaction     $  this->dbcon->commit(); } catch (Exception $  ex) {     // Rollback transaction if something goes wrong     $  this->dbcon->rollback();          $  this->errNum = $  this->dbcon->errno;     $  this->errText = $  this->dbcon->error;     return false; } 

When running $ stmt = $ this->dbcon->prepare($ query); the database raise an Err 1046: No database selected; however I did some other operations before that executed successfully, using the same DB connection.

This is the query I read with file_get_contents:

DELETE FROM `shares` WHERE `itemuuid` in (   WITH RECURSIVE files_paths (id, parent) AS   (     SELECT uuid, parentuuid       FROM core_data       WHERE uuid = ?     UNION ALL     SELECT e.uuid, e.parentuuid       FROM files_paths AS ep JOIN core_data AS e         ON ep.id = e.parentuuid   )   SELECT id FROM files_paths ) 

Note that is a recursive CTE query.

If I replace the $ query with a SELECT query, all the code runs correctly (no error 1046 raisen).

Any help or idea is appreciated.