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

Starting v2025, I decided not to disable Include common files option in order to get data from database into a Custom File. This is important, since v2025, PHPMaker strict to recommend us to always enable that option in order to use some built-in global functions.

So, if you want to load data from database into a Custom File in v2025, you may try the following.

  1. From demo2025 project, create a new Custom File, name it with test.php, and make sure you always enable Include common files option
  2. Copy the following code into Content section of that Custom File:
<?php

echo "<h3>Custom File with 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->fetchAssociative()) { // 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


?>

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

Well, although it is strongly not recommended to disable Include common files option for Custom File if you want to get data from database into a Custom File, however, as matter of fact, PHPMaker v2025 has still the ability to allow us to disable that option, after I reading this following post: What is the equiv. of loading system libraries? - #7 by yaaryvp

So, if you want to obviously to disable that option (again, it is strongly not recommended), here is the code to get data from database into a Custom File with disabling the Include common files option for v2025:

<?php
namespace PHPMaker2025\demo2025;

use DI\ContainerBuilder;

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

// Require files
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
require_once "src/userfn.php";

$containerBuilder = new ContainerBuilder();
$containerBuilder->useAttributes(true);

// Add definitions
$containerBuilder->addDefinitions("src/definitions.php");

// Dispatch container build event
DispatchEvent(new ContainerBuildEvent($containerBuilder), ContainerBuildEvent::NAME);

// Build PHP-DI container instance
$container = $containerBuilder->build();

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->fetchAssociative()) { // 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

?>