Jump to content

function seems to be working on one server, but not another


Go to solution Solved by Ch0cu3r,

Recommended Posts

****NOTE****

I have a workaround for this, listed at the end.  I'm self taught (OTJ) and am curious on why this works on one box and not another..

 

 

 

I have the below function, in a functions file, that works locally on my MAMP Server.  But when I upload it to our Web Dev (Linux) server I can't get past loading the functions file, so nothing opens..

 

MAMP PHP:  5.4.10

Web Server:  5.3.3

 

The line that's getting me is this.  Commenting it out things load fine for ALL pages.  I just can't display this value..

//Get results from returned object
$get_issue_info_value = $get_issue_info->fetch()['issue'];
    

Full function:

function get_issue_info($id){
           //echo '<script type="text/javascript">alert("inside get_issue_info function - the ID:  '.$id.'")</script>';       
             
           try{
            global $db;

            //Prepare Query
            $get_issue_info = $db->prepare("select issue from website_issues where id = :id");

            //Bind Values
            $get_issue_info->bindValue(':id', $id, PDO::PARAM_STR);
            
            //execute
            $get_issue_info->execute();
            
            //Get results from returned object
            $get_issue_info_value = $get_issue_info->fetch()['issue'];
            /*
             * Debug:  show returned value
             * echo '<script type="text/javascript">alert("Debug - Show value:  '.$get_issue_info_value.'")</script>';
             * 
             */
            //Return
            return $get_issue_info_value;

        }
        catch(PDOException $e)
            {
                echo 'ERROR inside the get_issue_info function: ' . $e->getMessage();
            }  
 
    }

For testing I ran the below code in the functions file locally, using MAMP, I get:

$get_issue_info_value = $get_issue_info->fetchAll(PDO::FETCH_ASSOC); 
Result:
Array
(
[0] => Array
(
[issue] => A new issue Added
)
 
)
$get_issue_info_value = $get_issue_info->fetch()['issue'];

Result:

A new issue Added

 

 

FIX - this works locally and on the Web Dev server

 

Fix in the functions file:

//Get results from returned object
//$get_issue_info_value = $get_issue_info->fetch(['issue']);
$get_issue_info_value = $get_issue_info->fetchAll(PDO::FETCH_ASSOC);

Fix in the php file:

$issue_info = get_issue_info($id);

/**  Check the returned value*/
echo "<pre>";
   print_r($issue_info);
echo "</pre>";

$issue = $issue_info[0]['issue'];
echo "<hr>";
/**  Check the returned value*/
echo "Issue: " . $issue;   

  • Solution

Function array dereferencing was added in PHP5.4. You web server uses PHP5.3, so you need to apply your fix

 

Or you could do

$tmp = $get_issue_info->fetch();
$get_issue_info_value = $tmp['issue'];
Edited by Ch0cu3r

Not quite sure, but I think that

 

$get_issue_info_value = $get_issue_info->fetch()['issue'];

Is a rather newly-available syntax that wasn't supported as long ago as 5.3.0.

 

edit: Boom. Cho0cu3r beat me to it.

Edited by dalecosp

Most likely because your servers have different PHP version. Accessing an array element directly from a function return value is a new syntax that requires 5.4 or newer.

//Requires PHP 5.4
$issue = $get_issue_info->fetch()['issue'];

//For older versions
$issue = $get_issue_info->fetch();
$issue = $issue['issue'];

Thank you - I was hoping it was a version issue.. A bit frustrating spending some time tracking that down... But, seeing that I don't control that other server and it's easier for me to spend the time verses a week of e-mails, and conversations explaining why I think we should upgrade that box..;)

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.