PHP request works in index.php, but not in custom module Topic is solved

Need help installing Joomla! 4.x? For all Joomla! 4.x installation issues please use this forum.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

PHP request works in index.php, but not in custom module

Post by Dada1991 » Wed May 18, 2022 9:38 am

Hello,

I try to do SQL request in Joomla V4.0.5. I'm using Postgresql.

My request:

Code: Select all

<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = ("SELECT * FROM current_weathers");
//$query->select($db->quoteName('temperature'));
//$query->from($db->quoteName('current_weathers'));

$db->setQuery($query);
$result = $db->loadRowList();
print_r($result);

?>
I try this request in my index.php --> It works.

Now I want to make this request in a custom module. I use the plugin Sourcerer v9.1.1 to use php code in my custom module. But when I make that request there, I got only ' 1 ' as result, whatever the request.

Any clue why a php request works in the index.php but no in a custom module ?
Last edited by toivo on Mon May 23, 2022 9:43 am, edited 2 times in total.
Reason: mod note: moved from 4.x Coding

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 14690
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP request works in index.php, but not in custom module

Post by toivo » Wed May 18, 2022 1:55 pm

If you create an installable module from scratch, any PHP code that works in index.php would work the same way in an installed module.

If you prefer to use Sourcerer, check the advice you received in your earlier topic Include PHP code in my custom module.
Toivo Talikka, Global Moderator

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Thu May 19, 2022 2:50 am

toivo wrote:
Wed May 18, 2022 1:55 pm
If you create an installable module from scratch, any PHP code that works in index.php would work the same way in an installed module.

If you prefer to use Sourcerer, check the advice you received in your earlier topic Include PHP code in my custom module.
Yes it should be worked as it's working in my index.php.
My Sourcerer is enabled.
In local computer, it's working well. It's only when I deployed on my production server it's not (maybe it's the reason ?).
I think my PHP is understood by my custom module (the php code is not displayed, only 1 is returned). But I don't understand why it return everytime 1 ? With print_r() or echo, nothing is displayed (even if it's not a fixed string)

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 14690
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP request works in index.php, but not in custom module

Post by toivo » Thu May 19, 2022 5:47 am

Dada1991 wrote:
Thu May 19, 2022 2:50 am
Yes it should be worked as it's working in my index.php.
My Sourcerer is enabled.
Your index.php is using just PHP and not Sourcerer. Did you check the advice from the earlier topic?
Toivo Talikka, Global Moderator

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Thu May 19, 2022 6:24 am

toivo wrote:
Thu May 19, 2022 5:47 am
Dada1991 wrote:
Thu May 19, 2022 2:50 am
Yes it should be worked as it's working in my index.php.
My Sourcerer is enabled.
Your index.php is using just PHP and not Sourcerer. Did you check the advice from the earlier topic?
Yes I was the owner of the topic.

Your answer helped that time, and it's working in my local computer. The problem is when I upload it on my production server, sourcerer return only '1'.

I just copy paste the code (working on my local computer but not on my production server).

Is it my connection with my production database that does't work ? I think not because from my index.php, I got result.
Is it my plugn that is not installed well ? if I disabled it, the php code is displayed, but if I enabled it, it return only 1 (so I guess the plugin is activated).

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 14690
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP request works in index.php, but not in custom module

Post by toivo » Thu May 19, 2022 7:38 am

Dada1991 wrote:
Thu May 19, 2022 6:24 am
Is it my connection with my production database that does't work ?
Try to list rows from some other table to make sure that the database connection works ok in Sourcerer.
Toivo Talikka, Global Moderator

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Thu May 19, 2022 9:09 am

toivo wrote:
Thu May 19, 2022 7:38 am
Dada1991 wrote:
Thu May 19, 2022 6:24 am
Is it my connection with my production database that does't work ?
Try to list rows from some other table to make sure that the database connection works ok in Sourcerer.
I try to know the parameter of my database using:

Code: Select all

{source}<?php
echo $db;
print_r($db);
?>{/source}
but it show only 1 as well.

By the way, even echo a string show 1 as well.

Code: Select all

{source}<?php
echo 'test';
?>{/source}
How can I know my database with sourcerer ?
It looks like the problem come from my plugin is not working, right?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 14690
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP request works in index.php, but not in custom module

Post by toivo » Thu May 19, 2022 10:39 am

Something wrong with the versions of Sourcerer, Joomla and PHP. Please post the results from the Forum Post Assistant (FPA) by following the instructions at https://forumpostassistant.github.io/docs so that our volunteer Joomla experts can review the configuration.
Toivo Talikka, Global Moderator

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Mon May 23, 2022 4:32 am

Hello again @toivo, and thanks for your help.

my version of my computer and of my production server (http://13.215.155.93/joomla/index.php) are the same:

- Sourcerer: v9.2.1
- Joomla: v4.0.5
- PHP: v7.4.3

On my computer it's working, but not on my production server.

Something is probably different between my computer and my production server, and make it impossible to do php request in my custom module. But I don't have idea yet, what can be different ?

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 14690
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP request works in index.php, but not in custom module

Post by toivo » Mon May 23, 2022 5:42 am

toivo wrote:
Thu May 19, 2022 10:39 am
Please post the results from the Forum Post Assistant (FPA) by following the instructions at https://forumpostassistant.github.io/docs so that our volunteer Joomla experts can review the configuration.
Toivo Talikka, Global Moderator

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Mon May 23, 2022 7:22 am

The report:
Forum Post Assistant (v1.6.5) : 23-May-2022 wrote:
Basic Environment :: wrote:Joomla! Instance :: Joomla! 4.0.5-Stable (Furaha) 11-December-2021
Joomla! Configured :: Yes | Read-Only (664) |
Configuration Options :: Offline: false | SEF: true | SEF Suffix: false | SEF ReWrite: false | .htaccess/web.config: No | GZip: false | Cache: false | CacheTime: 15 | CacheHandler: file | CachePlatformPrefix: false | FTP Layer: N/A | Proxy: false | LiveSite: | Session lifetime: 15 | Session handler: database | Shared sessions: false | SSL: 0 | Error Reporting: default | Site Debug: false | Language Debug: false | Default Access: Public | Unicode Slugs: false | dbConnection Type: pgsql | PHP Supports J! 4.0.5: Yes | Database Supports J! 4.0.5: Yes | Database Credentials Present: Yes |

Host Configuration :: OS: Linux | OS Version: 5.4.0-1018-aws | Technology: x86_64 | Web Server: Apache/2.4.41 (Ubuntu) | Encoding: gzip, deflate | System TMP Writable: Yes | Free Disk Space : 53.25 GiB |

PHP Configuration :: Version: 7.4.3 | PHP API: apache2handler | Session Path Writable: Yes | Display Errors: 0 | Error Reporting: 22527 | Log Errors To: | Last Known Error: | Register Globals: | Magic Quotes: | Safe Mode: | Allow url fopen: 1 | Open Base: | Uploads: 1 | Max. Upload Size: 2M | Max. POST Size: 8M | Max. Input Time: 60 | Max. Execution Time: 30 | Memory Limit: 128M

Database Configuration :: Version: 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1) (Client:12.9 (Ubuntu 12.9-0ubuntu0.20.04.1)) | Database Size: 7.79 MiB | #of Tables with config prefix:  69 | #of other Tables:  0 | User Privileges : Table Owner
Detailed Environment :: wrote:PHP Extensions :: Core (7.4.3) | date (7.4.3) | libxml (7.4.3) | openssl (7.4.3) | pcre (7.4.3) | zlib (7.4.3) | filter (7.4.3) | hash (7.4.3) | Reflection (7.4.3) | SPL (7.4.3) | session (7.4.3) | standard (7.4.3) | sodium (7.4.3) | apache2handler (7.4.3) | mysqlnd (mysqlnd 7.4.3) | PDO (7.4.3) | xml (7.4.3) | calendar (7.4.3) | ctype (7.4.3) | curl (7.4.3) | dom (20031129) | mbstring (7.4.3) | FFI (7.4.3) | fileinfo (7.4.3) | ftp (7.4.3) | gd (7.4.3) | gettext (7.4.3) | iconv (7.4.3) | intl (7.4.3) | json (7.4.3) | exif (7.4.3) | mysqli (7.4.3) | pdo_mysql (7.4.3) | pdo_pgsql (7.4.3) | pgsql (7.4.3) | Phar (7.4.3) | posix (7.4.3) | readline (7.4.3) | shmop (7.4.3) | SimpleXML (7.4.3) | soap (7.4.3) | sockets (7.4.3) | sysvmsg (7.4.3) | sysvsem (7.4.3) | sysvshm (7.4.3) | tokenizer (7.4.3) | xmlreader (7.4.3) | xmlrpc (7.4.3) | xmlwriter (7.4.3) | xsl (7.4.3) | zip (1.15.6) | Zend OPcache (7.4.3) | Zend Engine (3.4.0) |
Potential Missing Extensions ::
Disabled Functions :: pcntl_alarm | pcntl_fork | pcntl_waitpid | pcntl_wait | pcntl_wifexited | pcntl_wifstopped | pcntl_wifsignaled | pcntl_wifcontinued | pcntl_wexitstatus | pcntl_wtermsig | pcntl_wstopsig | pcntl_signal | pcntl_signal_get_handler | pcntl_signal_dispatch | pcntl_get_last_error | pcntl_strerror | pcntl_sigprocmask | pcntl_sigwaitinfo | pcntl_sigtimedwait | pcntl_exec | pcntl_getpriority | pcntl_setpriority | pcntl_async_signals | pcntl_unshare | |

Switch User Environment :: PHP CGI: No | Server SU: No | PHP SU: No | Potential Ownership Issues: Yes

Apache Modules :: core | mod_so | mod_watchdog | http_core | mod_log_config | mod_logio | mod_version | mod_unixd | mod_access_compat | mod_alias | mod_auth_basic | mod_authn_core | mod_authn_file | mod_authz_core | mod_authz_host | mod_authz_user | mod_autoindex | mod_deflate | mod_dir | mod_env | mod_filter | mod_headers | mod_mime | prefork | mod_negotiation | mod_php7 | mod_reqtimeout | mod_setenvif | mod_status | Apache/2.4.41 (Ubuntu) |
Potential Missing Modules :: mod_rewrite | mod_expires | mod_ssl |
Folder Permissions :: wrote:Core Folders :: images/ (775) | components/ (775) | modules/ (775) | plugins/ (777) | language/ (775) | templates/ (777) | cache/ (775) | logs/ (---) | tmp/ (775) | administrator/components/ (775) | administrator/modules/ (775) | administrator/language/ (775) | administrator/templates/ (775) | administrator/logs/ (775) |

Elevated Permissions (First 10) :: components/ (775) | components/com_users/ (775) | components/com_users/src/ (775) | components/com_users/src/Model/ (775) | components/com_users/src/Rule/ (775) | components/com_users/src/Service/ (775) | components/com_users/src/View/ (775) | components/com_users/src/View/Login/ (775) | components/com_users/src/View/Profile/ (775) | components/com_users/src/View/Remind/ (775) |
Extensions Discovered :: wrote:Components :: Site ::
Core ::
3rd Party::

Components :: Admin ::
Core :: com_users (4.0.0) 1 | com_plugins (4.0.0) 1 | com_menus (4.0.0) 1 | com_templates (4.0.0) 1 | com_newsfeeds (4.0.0) 1 | com_postinstall (4.0.0) 1 | com_privacy (3.9.0) 1 | com_workflow (4.0.0) 1 | com_modules (4.0.0) 1 | com_banners (4.0.0) 1 | com_wrapper (4.0.0) 1 | com_contenthistory (4.0.0) 1 | com_associations (4.0.0) 1 | com_finder (4.0.0) 1 | com_languages (4.0.0) 1 | com_login (4.0.0) 1 | com_ajax (4.0.0) 1 | com_cache (4.0.0) 1 | com_actionlogs (3.9.0) 1 | com_tags (4.0.0) 1 | com_config (4.0.0) 1 | com_checkin (4.0.0) 1 | com_installer (4.0.0) 1 | com_messages (4.0.0) 1 | com_content (4.0.0) 1 | com_media (3.0.0) 1 | com_joomlaupdate (4.0.3) 1 | com_cpanel (4.0.0) 1 | com_fields (4.0.0) 1 | com_admin (4.0.0) 1 | com_mails (4.0.0) 1 | com_categories (4.0.0) 1 | com_redirect (4.0.0) 1 |
3rd Party:: com_djimageslider (4.2) 1 | Hello World (0.0.6) 1 |

Modules :: Site ::
Core :: mod_footer (3.0.0) 1 | mod_articles_category (3.0.0) 1 | mod_articles_archive (3.0.0) 1 | mod_articles_news (3.0.0) 1 | mod_users_latest (3.0.0) 1 | mod_breadcrumbs (3.0.0) 1 | mod_tags_similar (3.1.0) 1 | mod_syndicate (3.0.0) 1 | mod_stats (3.0.0) 1 | mod_articles_popular (3.0.0) 1 | mod_languages (3.5.0) 1 | mod_whosonline (3.0.0) 1 | mod_random_image (3.0.0) 1 | mod_finder (3.0.0) 1 | mod_related_items (3.0.0) 1 | mod_tags_popular (3.1.0) 1 | mod_articles_categories (3.0.0) 1 | mod_banners (3.0.0) 1 | mod_menu (3.0.0) 1 | mod_custom (3.0.0) 1 | mod_login (3.0.0) 1 | mod_wrapper (3.0.0) 1 | mod_articles_latest (3.0.0) 1 | mod_feed (3.0.0) 1 |
3rd Party:: mod_jdsimplecontactform (1.9) 1 | DJ-ImageSlider (4.2) 1 |

Modules :: Admin ::
Core :: mod_stats_admin (3.0.0) 1 | mod_privacy_status (4.0.0) 1 | mod_popular (3.0.0) 1 | mod_latest (3.0.0) 1 | mod_frontend (4.0.0) 1 | mod_toolbar (3.0.0) 1 | mod_privacy_dashboard (3.9.0) 1 | mod_submenu (3.0.0) 1 | mod_sampledata (3.8.0) 1 | mod_loginsupport (4.0.0) 1 | mod_version (3.0.0) 1 | mod_user (4.0.0) 1 | mod_title (3.0.0) 1 | mod_menu (3.0.0) 1 | mod_custom (3.0.0) 1 | mod_login (3.0.0) 1 | mod_post_installation_messages (4.0.0) 1 | mod_multilangstatus (3.0.0) 1 | mod_feed (3.0.0) 1 | mod_latestactions (3.9.0) 1 | mod_logged (3.0.0) 1 | mod_quickicon (3.0.0) 1 | mod_messages (4.0.0) 1 |
3rd Party:: Import Content (1.0) 1 |

Libraries ::
Core ::
3rd Party:: Regular Labs Library (22.2.16083) 1 |

Plugins ::
Core :: plg_authentication_ldap (3.0.0) 0 | plg_authentication_joomla (3.0.0) 1 | plg_authentication_cookie (3.0.0) 1 | plg_user_profile (3.0.0) 0 | plg_user_contactcreator (3.0.0) 0 | plg_user_token (3.9.0) 1 | plg_user_joomla (3.0.0) 1 | plg_user_terms (3.9.0) 0 | plg_captcha_recaptcha (3.4.0) 0 | plg_captcha_recaptcha_invisible (3.8) 0 | plg_editors-xtd_pagebreak (3.0.0) 1 | plg_editors-xtd_readmore (3.0.0) 1 | plg_editors-xtd_article (3.0.0) 1 | plg_editors-xtd_module (3.5.0) 1 | plg_editors-xtd_fields (3.7.0) 1 | plg_editors-xtd_menu (3.7.0) 1 | plg_editors-xtd_image (3.0.0) 1 | plg_filesystem_local (4.0.0) 1 | plg_system_languagefilter (3.0.0) 1 | plg_system_sessiongc (3.8.6) 1 | plg_system_webauthn (4.0.0) 1 | plg_system_redirect (3.0.0) 0 | plg_system_actionlogs (3.9.0) 1 | plg_system_logrotation (3.9.0) 1 | plg_system_languagecode (3.0.0) 1 | plg_system_highlight (3.0.0) 1 | plg_system_accessibility (4.0.0) 0 | plg_system_privacyconsent (3.9.0) 0 | plg_system_cache (3.0.0) 0 | plg_system_skipto (4.0.0) 1 | plg_system_fields (3.7.0) 1 | plg_system_debug (3.0.0) 1 | plg_system_logout (3.0.0) 1 | plg_system_updatenotification (3.5.0) 1 | plg_system_sef (3.0.0) 1 | plg_system_remember (3.0.0) 1 | plg_system_httpheaders (4.0.0) 1 | plg_system_log (3.0.0) 1 | plg_system_stats (3.5.0) 1 | plg_actionlog_joomla (3.9.0) 1 | plg_twofactorauth_totp (3.2.0) 0 | plg_twofactorauth_yubikey (3.2.0) 0 | plg_installer_folderinstaller (3.6.0) 1 | plg_installer_webinstaller (4.0.0) 1 | plg_installer_override (4.0.0) 1 | plg_installer_urlinstaller (3.6.0) 1 | plg_installer_packageinstaller (3.6.0) 1 | plg_extension_joomla (3.0.0) 1 | plg_extension_namespacemap (4.0.0) 1 | plg_extension_finder (4.0.0) 1 | plg_finder_newsfeeds (3.0.0) 1 | plg_finder_tags (3.0.0) 1 | plg_finder_content (3.0.0) 1 | plg_finder_contacts (3.0.0) 1 | plg_finder_categories (3.0.0) 1 | plg_webservices_users (4.0.0) 1 | plg_webservices_messages (4.0.0) 1 | plg_webservices_newsfeeds (4.0.0) 1 | plg_webservices_redirect (4.0.0) 1 | plg_webservices_tags (4.0.0) 1 | plg_webservices_installer (4.0.0) 1 | plg_webservices_languages (4.0.0) 1 | plg_webservices_banners (4.0.0) 1 | plg_webservices_modules (4.0.0) 1 | plg_webservices_config (4.0.0) 1 | plg_webservices_menus (4.0.0) 1 | plg_webservices_content (4.0.0) 1 | plg_webservices_templates (4.0.0) 1 | plg_webservices_plugins (4.0.0) 1 | plg_webservices_privacy (4.0.0) 1 | plg_workflow_featuring (4.0.0) 1 | plg_workflow_notification (4.0.0) 1 | plg_workflow_publishing (4.0.0) 1 | plg_behaviour_versionable (4.0.0) 1 | plg_behaviour_taggable (4.0.0) 1 | plg_fields_imagelist (3.7.0) 1 | plg_fields_user (3.7.0) 1 | plg_fields_calendar (3.7.0) 1 | plg_fields_url (3.7.0) 1 | plg_fields_integer (3.7.0) 1 | plg_fields_editor (3.7.0) 1 | plg_fields_checkboxes (3.7.0) 1 | plg_fields_subform (4.0.0) 1 | plg_fields_textarea (3.7.0) 1 | plg_fields_sql (3.7.0) 1 | plg_fields_color (3.7.0) 1 | plg_fields_list (3.7.0) 1 | plg_fields_media (3.7.0) 1 | plg_fields_text (3.7.0) 1 | plg_fields_usergrouplist (3.7.0) 1 | plg_fields_radio (3.7.0) 1 | plg_media-action_resize (4.0.0) 1 | plg_media-action_crop (4.0.0) 1 | plg_media-action_rotate (4.0.0) 1 | plg_content_loadmodule (3.0.0) 1 | plg_content_pagebreak (3.0.0) 1 | plg_content_confirmconsent (3.9.0) 0 | plg_content_joomla (3.0.0) 1 | plg_content_pagenavigation (3.0.0) 1 | plg_content_vote (3.0.0) 0 | plg_content_finder (3.0.0) 1 | plg_content_fields (3.7.0) 1 | plg_content_emailcloak (3.0.0) 1 | plg_sampledata_multilang (4.0.0) 1 | plg_quickicon_overridecheck (4.0.0) 1 | plg_quickicon_extensionupdate (3.0.0) 1 | plg_quickicon_privacycheck (3.9.0) 1 | plg_quickicon_phpversioncheck (3.7.0) 1 | plg_quickicon_downloadkey (4.0.0) 1 | plg_quickicon_joomlaupdate (3.0.0) 1 | plg_api-authentication_token (4.0.0) 1 | plg_api-authentication_basic (4.0.0) 0 | plg_privacy_user (3.9.0) 1 | plg_privacy_consents (3.9.0) 1 | plg_privacy_actionlogs (3.9.0) 1 | plg_privacy_content (3.9.0) 1 | plg_privacy_message (3.9.0) 1 |
3rd Party:: PLG_EDITORS-XTD_SOURCERER (9.2.1) 1 | System - Regular Labs Installer (22.4.18687) ? | PLG_SYSTEM_SOURCERER (9.2.1) 1 | Smart Slider 3 System Plugin (3.5.1.3) 1 | PLG_SYSTEM_REGULARLABS (22.4.18687) 1 | Smart Slider 3 Updater Plugin (3.5.1.3) 1 | PLG_CONTENT_TTMENUSTYLE (1.0.0) 1 | plg_editors_codemirror (5.64.0) 1 | plg_editors_tinymce (5.10.2) 1 |
Templates Discovered :: wrote:Templates :: Site :: dmh_web_portal_template (1.0) 1 | jf_startupx (1.0.0) 1 | theme-20071-j4 (1.0) 1 | mod_search (1.7.0) ? | cassiopeia (1.0) 1 |
Templates :: Admin :: atum (1.0) 1 |

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: PHP request works in index.php, but not in custom module

Post by Dada1991 » Mon May 23, 2022 9:30 am

I find the solution ! I needed to change the owner of my joomla folder

sudo chown -R www-data:www-data /var/www/html/joomla/
sudo chmod -R 755 /var/www/html/joomla/

Thank you for your help, the report helped me to find the solution


Post Reply

Return to “Installation Joomla! 4.x”