For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > December 2007 > Fatal error when calling DB in function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Fatal error when calling DB in function
batterhead

2007-12-05, 4:15 am

Hi everyone,

I have a bit of code which to me, looks like it should work, but is
not and I am not sure why. Maybe someone here could help me. If I
process this query outside of a function it works just fine. If I call
it using a function as show below I get the error:

Fatal error: Call to a member function on a non-object in /Library/WebServer/...

<?php
require_once("include/dbConnect.inc.php");

function getProps() {
$res = $db->query('SELECT * FROM properties');
while ($res->fetchInto($prop)) {
echo $prop->id . "<br />";
}
$res->free();
}

getProps();

?>
Carsten Wiedmann

2007-12-05, 4:15 am

batterhead schrieb:

> If I
> process this query outside of a function it works just fine. If I call
> it using a function as show below I get the error:
>
> Fatal error: Call to a member function on a non-object in /Library/WebServer/...


You find the answer here:
http://www.php.net/manual/en/langua...ables.scope.php

> <?php
> require_once("include/dbConnect.inc.php");
>
> function getProps() {


e.g. this can help:
| global $db;

> $res = $db->query('SELECT * FROM properties');
> while ($res->fetchInto($prop)) {
> echo $prop->id . "<br />";
> }
> $res->free();
> }
>
> getProps();
>
> ?>


Regards,
Carsten
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com