How can I display sql query from database? I'm trying to display sql query on page in drupal 8.2, but I get an error:


  The controller must return a response (Object(Drupal\Core\Render\Markup) given).


How should I change the following code?

if ($result = $mysqli-&gt;query("SELECT id, discipline, teacher FROM canteach")) {
  $output .= "Select вернул " . $result-&gt;num_rows . " строк &lt;br /&gt;";
}

$mysqli-&gt;close();

$header = array('id', 'Discipline', 'Teacher');
$rows = array();

while ($row = $result-&gt;fetch_array(MYSQLI_NUM)) {
  $rows[] = array($row-&gt;id, $row-&gt;discipline, $row-&gt;teacher);
}

$result-&gt;close();

$items_array = array(
  '#theme' =&gt; 'table',
  '#items' =&gt; array('header' =&gt; $header, 'rows' =&gt; $rows),
);

return render($items_array);
From the error, I assume your code is in a controller? In that case, take note that you should return a render array or an object inheriting the Response class, not HTML.

In your case, just do:

return $items_array;


instead of 

return render($items_array);


But wait!

The table render array should be constructed this way:

$items_array = array(
  '#theme' =&gt; 'table',
  '#header' =&gt; $header, 
  '#rows' =&gt; $rows,
); 


For a reference of what you can pass in, check out https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21table.html.twig/8.1.x

Other notes

The way you are querying data is not wrong per se but definitely not the "Drupal way". To perform queries using Drupal's DBAL, check out https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!database.api.php/group/database/8.2.x