How to Load Data from Database into Custom Files (v2024)

This is the updated version for PHPMaker v2024, from similar topic for PHPMaker v2021: How to Load Data from Database into Custom Files (v2021).Now we can also use Custom Files with Include common files DISABLED for PHPMaker v2024.

  1. Make sure you have already updated template to the latest version from Tools → Update Template.
  2. Create a Custom File and name it as test.php, then copy and paste this code into Content section of the Custom File of demo2024 project of PHPMaker 2024 application:


<?php

namespace PHPMaker2024\demo2024;

use PHPMaker2024\demo2024\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler, RouteAttributes};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Middlewares\Whoops;
use Dflydev\DotAccessData\Data;

global $RELATIVE_PATH;

// Autoload
require_once "vendor/autoload.php";

// Require files
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
$ConfigData = new Data($CONFIG); // Ensure that $ConfigData is accessible by Global Codes
require_once "src/userfn.php";

echo "<h3>Custom File without Include common files option</h3>";

$sql = "SELECT `Model` FROM `models`"; // define your SQL
$stmt = ExecuteQuery($sql); // execute the query
$value = ""; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
while ($row = $stmt->fetch()) { // loop
$value .= $row["Model"] . "<br>"; // in case the result returns more than one record, display it and separated by line break
} // end loop
echo "Here is the Models list: <br>" . $value; // display the result
} else { // if there are no result
echo "No record found."; // display the message
} // end of check condition


?>



  1. Re-generate ALL the script files, as usual.
  2. Visit the Custom File from browser: http://localhost/demo2024/test.php

The Limitation: The styles, header, footer, sidebar won’t be implemented in this Custom File.This trick is useful if you want to optimize the built-in functions regardless of the appearance / layout of your web application.

Hi,
First, let me thank you for your great work.
I used your suggested code for 2024 and I have a problem. I can’t call and use php classes that I placed in global code section of my project (userfn.php).
e.g.

<?php
namespace PHPMaker2024\TH;
use PHPMaker2024\TH\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler, RouteAttributes};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Middlewares\Whoops;
use Dflydev\DotAccessData\Data;
global $RELATIVE_PATH;
// Autoload
require_once "vendor/autoload.php";
// Require files
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
$ConfigData = new Data($CONFIG); // Ensure that $ConfigData is accessible by Global Codes
require_once "src/userfn.php";
	$a = new Tamin; // call the class
	$a->printHelloWord(); // print Hello Word
?>

Thanks

Try to change this part:
$a = new Tamin; // call the classto:
$a = new \Tamin; // call the classthen try again.

Thank you, solved.
Another Question:
how to Ensure that $ConfigData is accessible by Global Codes? is any configuration option for this case?
Thanks

No. You just call the setting that related to that $ConfigData.

For example, you want to display the environment setting that related to that config data, simply put this following code:$environment = $ConfigData[“ENVIRONMENT”];After that, you may just echo that $environment variable to display the value.

is there a update for v2025?

thanks

Hi mobhar

Thanks for code working great :slight_smile:

Unfortunately, in v2025 (and other recent versions), it is strongly not recommended to disable Include common files option anymore.

You should always enable that option in order to use the global functions that provided by PHPMaker: How to Load Data from Database into Custom File (v2025)

If you only login the user and do something without any HTML in response, you better use Route_Action server event.

Hi @mobhar ,

Your code works well however i want to use some security function in phpmaker2024 only like isLoggedIn() or CurrentUserName()
i dont get error and no response too

when i try $Security->isLoggedIn() or $Security->isAdmin() then i get error

Fatal error : Uncaught Error: Call to a member function isLoggedIn() on null in pathto.php:35 Stack trace: #0 {main} thrown in pathto.php on line 35

Any suggestion please

Thanks

Try to use Security() instead of $Security.

Or you may simply use IsLoggedIn() and IsAdmin() global functions.

Hi @mobhar

using Security() is throwing error " Uncaught Error: Undefined constant “PHPMaker2024<projectname>\Security” in D:\xampp\htdocs\filepath.php:36 Stack trace: #0 {main} thrown in D:\xampp\htdocs\filepath.php on line 36"

and if i try directly

IsLoggedIn() and IsAdmin() → not throwing any error nor returning any data

Thanks

You better post your related code for discussion.

Hi @mobhar
Thanks for reply

<?php

namespace PHPMaker2024\jam_v3;
error_reporting(E_ALL);
ini_set('display_errors', 1);
$xprocess = &$Page;

use PHPMaker2024\jam_v3\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler, RouteAttributes};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Middlewares\Whoops;
use Dflydev\DotAccessData\Data;

global $RELATIVE_PATH;

// Autoload
require_once "vendor/autoload.php";

// Require files
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
$ConfigData = new Data($CONFIG); // Ensure that $ConfigData is accessible by Global Codes
require_once "src/userfn.php";

echo "<h3>Custom File without Include common files option</h3>";
echo  Security;
echo CurrentUserID();
echo isAdmin();

%>
Usernme : <?=CurrentUserName()?>


Above is the code requested for review

You should double check your code above.

echo Security; 

is wrong. What is that for? You should remove it since it useless, and it will raise an error as you mentioned above.

What is %> for? You should remove it, too. This is PHP, not the other language.

In addition, this code:

Usernme : <?=CurrentUserName()?>

should be:

echo "Username: " . CurrentUserName(); 

?>

Hi @mobhar

Thanks for pointing out %? its a typo from my side

<?php

namespace PHPMaker2024\jam_v3;
error_reporting(E_ALL);
ini_set('display_errors', 1);
$xprocess = &$Page;

use PHPMaker2024\jam_v3\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler, RouteAttributes};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Middlewares\Whoops;
use Dflydev\DotAccessData\Data;

global $RELATIVE_PATH;

// Autoload
require_once "vendor/autoload.php";

// Require files
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
$ConfigData = new Data($CONFIG); // Ensure that $ConfigData is accessible by Global Codes
require_once "src/userfn.php";

echo "<h3>Custom File without Include common files option</h3>";
echo CurrentUserID();
echo isAdmin();

echo "Username: " . CurrentUserName(); 

?>

Usernme : <?=CurrentUserName()?> → core php but i modified as you suggested

Prints only text Username as output nothing else , kindly check the image

Thanks

Although there is no error, you cannot display current username by calling CurrentUserName() global function in a Custom File with Include common files option disabled. This is one of some limitation if you disable the option for your custom file.

Sounds good,

Thank you @mobhar :slight_smile: