Below you can find a script that loads the joomla 4 (only 4) framework on an external PHP page
If you have to make a query that uses both 'where' and 'and', for instance you have to make a selection that has 2 variables, you can do this in the following manner:
// load J4 framework in external page
// **************************************************************************
//No Direct Access
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__ . '/../../..');
require JPATH_BASE . '/includes/defines.php';
require JPATH_BASE . '/includes/framework.php';
// Boot the DI container.
$container = \Joomla\CMS\Factory::getContainer();
// Alias the session service key to the web session service.
$container->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
// Get the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
// j4 framework loaded
// **************************************************************************
// define namespaces
// **************************************************************************
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;
Happy coding!