Jump to content

Recommended Posts

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

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.

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.

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.