include statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

include statement

Post by ModusPonens »

Not sure if I'm having a problem with my ini file or if there's something wrong with the structure of my programming.

abc.php
<?PHP
include ("def.php");

doDB();

$result = mysqli_query($Con, "call sp_getdata;");
.
.
.
?>

def.php
<?PHP
function doDB
{
global $Con;

$Con = mysqli_connect("localhost", "user", "password", "db");
.
.
.
}
?>

It's bombing on the bold line. I'm getting a recordset error, which tells me that $Con is not getting executed from the include file. I've changed the include path in the ini file several times. What am I overlooking?
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: include statement

Post by fugix »

i believe that "function doDB" should be "function doDB()"
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: include statement

Post by ModusPonens »

I know, I omitted the parens for brevity. they're in the code.

The frustrating thing is that if I instance the connection and execute the query from within "abc.php" it executes the way it is supposed to execute.

The actual message I'm getting is "PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in..." All I'm doing is populating some drop down list boxes. When I instance from within abc.php, the drop-downs fill properly, when I instance from the include file, they don't.

So the error is occurring at the line where I store the query results in $result, which invokes the connection object $Con.

Even added ini_set("include_path", "C:\web\watchmyteendrive"); above the include statement, and still nothing. I must be having a problem in a place where I'm not looking. I'm wondering if there's not something else in the php.ini that needs to be modified.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: include statement

Post by fugix »

the only other thing that i can think of is to include the direct path to you def.php file...
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: include statement

Post by ModusPonens »

fugix wrote:the only other thing that i can think of is to include the direct path to you def.php file...
I did that with the ini_set above the include statement. Shouldn't make any difference, but I've written the include and the ini_set statements in the general code area outside the the function that makes the call to the function in the included function.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: include statement

Post by fugix »

i would set the $con value in the def.php and call it out on your other page...to make sure that the include is working properly
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: include statement

Post by ModusPonens »

fugix wrote:i would set the $con value in the def.php and call it out on your other page...to make sure that the include is working properly
Set up a dummy php page that only called the included page. Set $Con = "string";, then tried to echo $Con from the calling page. Not working. Has to be one of the config files, but I have no idea, other than the include_path, what I need to set.
ModusPonens
Forum Newbie
Posts: 13
Joined: Sat Apr 09, 2011 11:57 am

Re: include statement

Post by ModusPonens »

All right, figured out at least part of the problem. For some reason, the global declaration doesn't work. If I declare $Con global in the <head> tag on the calling page, or within the function in the included page, fetching the recordset bombs. If I just declare $Con locally in the called function from the include page, then pass the connection object back to the calling function, it works. :banghead:
Post Reply