Can't get user-defined function to work

Hello,

For some reason, I can't get my function to work. Have looked to see if something was not set right in the php.ini file. I keep coming up with nothing, so maybe someone can see what I am blind to.

Function is to get templates from mysql. I am including it into another file. Nothing too fancy. It has always work on any server, and works on my localhost I used to develop with. But not my linode.

Ubuntu 10.04 lts, Apache 2, Php 5, Mysql(?).

Any help appreciated.

9 Replies

I can take out the code in the function, put it on the page and it will load as it should. So I know mysql is good… just seems to be something up with functions?

functions.php >>

function mytemplate($templatename) {
    global $templatecache;
        if ($templatecache[$templatename]!="") {
            $template = $templatecache[$templatename];
        } else {
            $query = mysql_query("SELECT value FROM templates WHERE name='$templatename'");
            if (mysql_num_rows($query)!=0) {
                $template = mysql_result($query,"value");
                $template = str_replace("\"","\\\"",$template);
                $templatecache[$name] = $template;
            } else { 
                    $template = "No Template: **$templatename** Found!";
                }
            }
    return $template;
}

And test.php >>

The lines above commented out (//) will work, but the function won't. Actually NO function will work…

Sanity check: is functions.php in the inc/ directory?

Yes functions.php is in /inc. I know it's including because I put in a variable in functions.php and it echos on test.php.

change

$templatecache[$name] = $template; 

to $templatecache[$templatename] = $template;

Thanks for your help guys. I tried changing $templatecache[$templatename] variable, but did not work.

I got looking over my installation and I followed the LAMP guide here @ the library. I did install php5-suhosin. So I have been looking into that as a possible.

In my /var/log/apache2/error.log >>

PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/apache2/conf.d/mcrypt.ini on line 1 in Unknown on line 0
[Mon Aug 08 17:47:21 2011] [notice] Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.9 with Suhosin-Patch mod_ssl/2.2.14 OpenSSL/0.9.8k configured -- resuming normal operations
[Mon Aug 08 17:49:08 2011] [notice] caught SIGTERM, shutting down

I corrected the # to a ; in mcrypt.ini. Also looked in my php.ini & suhosin.ini files… All of the lines in the suhosin.ini file were ';' commented out. I tried turning on: >>

suhosin.executor.disable_eval = off
suhosin.simulation = on

But it did not work. And I don't see any mention of suhosin in php.ini or apache2.conf anywhere. Think this suhosin extension isn;t configured properly and to blame?

````
; configuration for php suhosin module
extension=suhosin.so

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
; the following values are the internal default settings and set implicit
; feel free to modify to your needs
; documentation can be found at:
; http://www.hardened-php.net/suhosin/configuration.html

[suhosin]
; Logging Configuration
;suhosin.log.syslog =
;suhosin.log.syslog.facility = 9
;suhosin.log.syslog.priority = 1
;suhosin.log.sapi =
;suhosin.log.script = 0
;suhosin.log.phpscript = 0
;suhosin.log.script.name =
;suhosin.log.phpscript.name =
;suhosin.log.use-x-forwarded-for = off

; Executor Options
;suhosin.executor.maxdepth = 0 ;suhosin.executor.include.maxtraversal = 0
;suhosin.executor.include.whitelist =
;suhosin.executor.include.blacklist =
;suhosin.executor.include.allowwritablefiles = on
;suhosin.executor.func.whitelist =
;suhosin.executor.func.blacklist =
;suhosin.executor.eval.whitelist =
;suhosin.executor.eval.blacklist =
;suhosin.executor.disableeval = off ;suhosin.executor.disableemodifier = off
;suhosin.executor.allow_symlink = off

; Misc Options
;suhosin.simulation = on
;suhosin.apcbugworkaround = off
;suhosin.sql.bailoutonerror = off
;suhosin.sql.userprefix = ;suhosin.sql.userpostfix =
;suhosin.multiheader = off
;suhosin.mail.protect = 0
;suhosin.memory_limit = 0

; Transparent Encryption Options
;suhosin.session.encrypt = on
;suhosin.session.cryptkey =
;suhosin.session.cryptua = on
;suhosin.session.cryptdocroot = on
;suhosin.session.cryptraddr = 0
;suhosin.session.checkraddr = 0
;suhosin.cookie.encrypt = on
;suhosin.cookie.cryptkey =
;suhosin.cookie.cryptua = on
;suhosin.cookie.cryptdocroot = on
;suhosin.cookie.cryptraddr = 0
;suhosin.cookie.checkraddr = 0
;suhosin.cookie.cryptlist =
;suhosin.cookie.plainlist =

; Randomness
;suhosin.srand.ignore = on
;suhosin.mt_srand.ignore = on

; Filtering Options
;suhosin.filter.action =
;suhosin.cookie.maxarraydepth = 100
;suhosin.cookie.maxarrayindexlength = 64 ;suhosin.cookie.maxnamelength = 64 ;suhosin.cookie.maxtotalnamelength = 256 ;suhosin.cookie.maxvaluelength = 10000 ;suhosin.cookie.maxvars = 100
;suhosin.cookie.disallownul = on ;suhosin.get.maxarraydepth = 50 ;suhosin.get.maxarrayindexlength = 64
;suhosin.get.maxnamelength = 64
;suhosin.get.maxtotalnamelength = 256
;suhosin.get.maxvaluelength = 512
;suhosin.get.maxvars = 100 ;suhosin.get.disallownul = on
;suhosin.post.maxarraydepth = 100
;suhosin.post.maxarrayindexlength = 64 ;suhosin.post.maxnamelength = 64 ;suhosin.post.maxtotalnamelength = 256 ;suhosin.post.maxvaluelength = 1000000 ;suhosin.post.maxvars = 1000
;suhosin.post.disallownul = on ;suhosin.request.maxarraydepth = 100 ;suhosin.request.maxarrayindexlength = 64
;suhosin.request.maxtotalnamelength = 256
;suhosin.request.maxvaluelength = 1000000
;suhosin.request.maxvars = 1000 ;suhosin.request.maxvarnamelength = 64 ;suhosin.request.disallownul = on
;suhosin.server.encode = on
;suhosin.server.strip = on
;suhosin.upload.maxuploads = 25 ;suhosin.upload.disallowelf = on
;suhosin.upload.disallowbinary = off ;suhosin.upload.removebinary = off
;suhosin.upload.verificationscript = ;suhosin.session.maxid_length = 128
;suhosin.coredump = off
;suhosin.protectkey = 1
;suhosin.stealth = 1
;suhosin.perdir = "0"

````

First, I believe the function mysqlresult is incorrectly written. The second parameter of mysqlresult is a row number, so…
> $template = mysqlresult($query,"value"); should become something like
> $template = mysql
result($query,0,"value");

Secondly, why do you need to use an eval()? Can't you just do something like:
> echo mytemplate("tpl-a");

If all the suggestions above have been tried, have you tried doing an echo inside the function to check if the function runs in the first place? So, echoing out the value of $template within the function instead of returning the value.

Good luck!

> $template = mysql_result($query, 0, "value");
Fixed my issue. Thank you & everyone who helped me out.

I was using eval to use variables in my template (from the mysql db).

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct