Jump to content

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


skygremlin

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;   

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.

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..;)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.