Jump to content

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


skygremlin
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;   

Link to comment
Share on other sites

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'];
Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.