Jump to content

Can't understand why my function won't work...


ibanez270dx

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.

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.