ibanez270dx Posted November 5, 2010 Share Posted November 5, 2010 Hi everyone, It's been awhile since I've coded in PHP and I can't figure out whats wrong with my code... I have a simple function to grab a field from my database and return it. I can make it work without the function, but when I implement it as a function, it breaks - no error, just a blank page. This works fine: $page = "page_about"; $sql = "SELECT body FROM " . $page . " WHERE id=1"; $result = @mysql_query($sql,$connection) or die(mysql_error()); $row = mysql_fetch_array($result); $display = $row[0]; But this does not: function grabBody($page) { $sql = "SELECT body FROM " . $page . " WHERE id=1"; $result = @mysql_query($sql,$connection) or die(mysql_error()); $row = mysql_fetch_array($result); return $row[0]; } $display = grabBody("page_about"); If anyone has any idea whats wrong, please let me know!! Thank you, - Jeff Miller Quote Link to comment https://forums.phpfreaks.com/topic/217809-cant-understand-why-my-function-wont-work/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2010 Share Posted November 5, 2010 You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. You will save a ton of time. Quote Link to comment https://forums.phpfreaks.com/topic/217809-cant-understand-why-my-function-wont-work/#findComment-1130530 Share on other sites More sharing options...
DavidAM Posted November 5, 2010 Share Posted November 5, 2010 Also remove the '@' sign and erase it from your memory. Hiding errors does not help you. The error you should see is that $connection is not defined. It might be defined outside your function but it is not defined inside the function. Quote Link to comment https://forums.phpfreaks.com/topic/217809-cant-understand-why-my-function-wont-work/#findComment-1130534 Share on other sites More sharing options...
ibanez270dx Posted November 5, 2010 Author Share Posted November 5, 2010 Ah, thanks you guys! I enabled display_errors and, as predicted, got the error about $connection not being defined within the function. Thanks again, I appreciate the help! - Jeff Quote Link to comment https://forums.phpfreaks.com/topic/217809-cant-understand-why-my-function-wont-work/#findComment-1130545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.