Global Variable not arrive at Custom File?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
Bishu
User
Posts: 429

Global Variable not arrive at Custom File?

Post by Bishu »

My project is on Microsoft SQL and in codding I need to connect to mysql manually. So I have use the code as -
Global->All Pages->Page_Loading
global $conn2;
$conn2 = ADONewConnection('mysqlt');
$conn2->port = 3306;
$conn2->Connect($hostname, $username, $password, $database);

And in the table itself I use the following code which work fine -
global $conn2;
$conn2->Execute("");
$conn2->GetOne("");

But when I use this same code in the custom file I got an error as -
Fatal error: Call to a member function Execute() on a non-object in C:\wamp\www\

I have selected the "Include common file" while creating the custom file.
I have also check the generated code of the custom file and I found
<?php include_once $EW_RELATIVE_PATH . "userfn11.php" ?>

m very confuse why my code is not working only on custom file.


mobhar
User
Posts: 11756

Post by mobhar »

Yes, you are right. The reason for this since the custom file is excluded for calling "Page_Loading" server event in PHPMaker template.

To solve this issue, then simply change this from "phpcommon-scripts.php" in the template file:
<!--##
if (CTRL.CtrlType.toLowerCase() != "field" && CTRL.CtrlID != "custom") {
if (SYSTEMFUNCTIONS.ServerScriptExist("Global","Page_Loading")) {
##-->

become:
<!--##
if (CTRL.CtrlType.toLowerCase() != "field") {
if (SYSTEMFUNCTIONS.ServerScriptExist("Global","Page_Loading")) {
##-->


Webmaster
User
Posts: 9430

Post by Webmaster »

Since it is custom file, there is no need to use Page_Loading event or to add the event in the page, you can add your code in the Custom File directly or call the event directly.

In fact, in this case, putting that code in the Page_Loading event means you want to have the custom connection in all pages, then it is simpler to put it in server side Global Code directly and the connection will be available in all page without the need of event.


mobhar
User
Posts: 11756

Post by mobhar »

Even "Global Code" has the similar function like "Page_Loading" server event, however, we recently often use "Page_Loading" to make sure that this server event also will be implemented also for the custom files, as well as for the generated files from PHPMaker.

In other words, we often to put the code in "Page_Loading" (not in "Global Code"), since we want the same behavior in the generated page also will be implemented for the custom files. Hopefully this makes sense.


Bishu
User
Posts: 429

Post by Bishu »

I agree with @mobhar as the custom file also included in the PHPMaker project itself.

my problem has solve my changing the template file of "phpcommon-scripts.php"
but as per my practice I do not want to change the template file because it make very difficult when we upgrade our PHPMaker version.

So is there anyway to do this by using extension.


Webmaster
User
Posts: 9430

Post by Webmaster »

No need to customize template or make extension at all. Webmaster wrote:
In fact, in this case, putting that code in the Page_Loading event means you want to have the custom connection in all pages, then it is simpler to put it in server
side Global Code directly and the connection will be available in all page without the need of event.

It is also simpler to the use database helper.


Post Reply