SQL request in php file called with ajax

Hello,
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>

$('#button').click(function(){
	$.ajax({
		url: 'test.php',
		type: 'POST',
		dataType : 'html',
		success: function(data) {
			$("#test").html(data);
		}
	});
});
			
	</script>

My page test.php

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

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