SQL request in php file called with ajax

for simplicity I’ve a page.tpl.php with ajax code :

  • The ajax code call a php page named “test.php”
  • This php page contains a sql request with an external database connection

My page.tpl.php

<div id="test"></div>

		url: 'test.php',
		type: 'POST',
		dataType : 'html',
		success: function(data) {

My page test.php

	$result = db_query('SELECT * FROM {finalite} ORDER BY id');
	foreach ($result as $record) {
		print $record->title;

The sql request works in page.tpl.php but not in the php file ‘test.php” called by ajax
error message “Fatal error: Call to undefined function db_set_active() in…………..”

Drupal version: 

Source: https://www.drupal.org/taxonomy/term/4/feed