skygremlin Posted December 11, 2013 Share Posted December 11, 2013 ****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 https://forums.phpfreaks.com/topic/284710-function-seems-to-be-working-on-one-server-but-not-another/ Share on other sites More sharing options...
Ch0cu3r Posted December 11, 2013 Share Posted December 11, 2013 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']; Link to comment https://forums.phpfreaks.com/topic/284710-function-seems-to-be-working-on-one-server-but-not-another/#findComment-1462101 Share on other sites More sharing options...
dalecosp Posted December 11, 2013 Share Posted December 11, 2013 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. Link to comment https://forums.phpfreaks.com/topic/284710-function-seems-to-be-working-on-one-server-but-not-another/#findComment-1462102 Share on other sites More sharing options...
kicken Posted December 11, 2013 Share Posted December 11, 2013 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 https://forums.phpfreaks.com/topic/284710-function-seems-to-be-working-on-one-server-but-not-another/#findComment-1462103 Share on other sites More sharing options...
skygremlin Posted December 11, 2013 Author Share Posted December 11, 2013 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 https://forums.phpfreaks.com/topic/284710-function-seems-to-be-working-on-one-server-but-not-another/#findComment-1462129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.