using include<

liunx

Guest
Hi I want to create a function that handles connecting to the database and have it on functions.php page and then include that page on all pages that require a db connection. make sense?

How do I go about this?

I have this function on a page called function.php


<?php
function db_connect()
{
$connstr = "dbname = site user = user host = localhost port = 5432 password = pwd ";
$dbh=pg_connect($connstr);
if (!$dbh)
{
echo "no connection to database has been established";
}
)
?>

I then use

include ("inc/functions.php");


I call the function on the page

db_connect()

but get an error undefined function.


any help appreciated.did you check to make sure the file exists?

include("/exact/path/to/file/filename.ext");

also...your function would be better as:

<?php
function db_connect()
{
$connstr = "dbname = site user = user host = localhost port = 5432 password = pwd ";
$dbh=pg_connect($connstr);
if (!$dbh)
{
die("no connection to database has been established");
}
)
return true;
?>


and lastly...the code has to be the following:

include("/exact/path/to/file/filename.ext");
db_connect();Thanks for the reply.

The functions.php file/page is in the same directory as the rest of the site.../var/www/html/site so do I need to put

include ("inc/var/www/html/site/functions.php");

heres the actual error message.

Fatal error: Call to undefined function: db_connect() in /var/www/html/Databasesite2/Login2.php on line 15

Now I', getting this error message as well lol

Warning: Failed opening 'inc/functions.php' for inclusion (include_path='.:/usr/share/pear') in /var/www/html/Databasesite2/Login2.php on line 2
Does this mean that include path has been set to /usr/share/pear directory?
is that where I need to put my include files?
I'm working with redhat 9 defaults here everything installed via packet manager nothing compiled from source.

hope this isn't to garbled

Bobif it's in the same dir...just use include("functions.php");

..I can't see a file's path being /inc/var/www/html....on every server I've ever worked on that ran red hat linux(all of the ones I've ever worked on as well as our current hosting server :P) have a path like:

/home/username/var/www/html

unless inc is the username..in which case it will only work if you use /home/ before your path.But in this case it's not needed since it's in the same dir.The warning means the file to be included couldn't be found/opened...so change the include(); call accordingly and it should work....the function undefined error is based on the fact the function code was never included.ok tried ("functions.php") that worked
but returned this error

Warning: pg_connect() unable to connect to PostgreSQL server: FATAL: Database
"Databasesite2" does not exist in the system catalog. in /var/www/html/Databasesite2/functions.php
on line 5
no connection to database has been established
Couldn't connect to database

so tried putting the 'functions.php' file in /var/www/html but this returned same error.but with different path.

this is what function looks like line 5 is the $dbh=...
<?php
<?php
function db_connect()
{
$connstr = "dbname =Databasesite2 user = bob host = localhost port = 5432 password = pwd ";
$dbh=pg_connect($connstr);
if (!$dbh)
{
echo "no connection to database has been established";
}
}
return true;
?>are you sure the db exist? you've got the right password and username for it? sounds stupid but you have no idea how many times i did that :PDuh was using the site name instead of db name:o

evrything is ok now.
thanks for the help peeps.

BobHmm well not quite:confused:

I can connect to db using$connstr = "dbname = HelpData user = $userID host = localhost port = 5432 password = $password ";
$dbh=pg_connect($connstr);
if (!$dbh)
{
die ("no connection to database has been established");
} a session is started and session variables set and the user gets passed to another page.

when I use the above code in a function and call it the connection is established but doesnt pass the user to the next page seems to stay in the ("functions.php").

Bobadd global $dbh; to the function.

function db_connect(){
global $dbh;

rest of code...thanks scoutt
I had done that for the $userid and $password so var value could come in. forgot I had to do it to allow var value out.

BobHi,

Seemed to make more sense to re-awake this thread rather than start another.

I have a very similar problem. I have base folder where index.php and othermain pages reside. I also have an include folder in which resides constant html etc and a dbfunctions.php. Back in the base folder I have a forums folder in which resides all that belongs to phpBB.

Now, I have modified some of the files to make sure that the forum is properly integrated into the rest of the site. All was fine and dandy. However I then modified one of the main page included files to bring up some names from the db. Unfortunately I have a Fatal error: Call to undefined function error message for the function getRS(). I have included the file properly (I wrote an additional echo 'boo<br />'; at the end of the file to check this ... as predicted it writes boo at the top of the page) and the function has been used countless times around the rest of the site with no problems.

So if the function is there and it works fine, why do I get this error? Below is the code for the function.

function getConnectionString()
{
mysql_connect("localhost", "root", "password");
@mysql_select_db("ereview") or die("Unable to select database");
}
function getRS($sqlquery)
{
getConnectionString();
@$result = mysql_query($sqlquery) or showError($sqlquery);
return $result;
}
function showError($sql)
{
$debug = getRS("select debug from admin_options");
if(mysql_result($debug, 0, "debug") == 1)
die("<b>Bad SQL statement:</b><br />" . $sql);
else
die("There was an error accessing the database.");
}

I could copy and paste the html and php directly from the included files into the file that requires them but this is ofcourse not ideal in the case that I want to change it and reflect the changes all over the site.

Suggestions?probably cause of this

@$result = mysql_query($sqlquery) or showError($sqlquery);

the @ sign is wrong. it can't go on the variable, it has to go on the function

$result = @mysql_query($sqlquery) or showError($sqlquery);thanks for the correction but nope, no change.
if this is another IIS thing I might flip - but even then it doesnt make sense why it works on all other pages but not on the page which also has phpBB forums in.:shady:actually, you have a nasty loop there. I am surprised it works at all.

function getRS($sqlquery)
{
getConnectionString();
@$result = mysql_query($sqlquery) or showError($sqlquery);
return $result;
}
function showError($sql)
{
$debug = getRS("select debug from admin_options");
if(mysql_result($debug, 0, "debug") == 1)
die("<b>Bad SQL statement:</b><br />" . $sql);
else
die("There was an error accessing the database.");
}

lets follow this. someplace I can imagine you call getRS($sqlquery) outisde those functions. ok, the query fails so it calls showError(), which in turn calls getRS() again and just keeps looping, if the wuery is failing. so lets try this


function getRS($sqlquery)
{
getConnectionString();
@$result = mysql_query($sqlquery) or die(mysql_error());
return $result;
}

just to see if it is failing.

if not than it maybe a include problem. how are you including it?i see what you mean but I'm not sure that can happen. For each sql query it has to open a connection using getConnectionString() which does make use of the die() method. Admittedly I should put an additional or die() for the mysql_connect(). Anyway, the connection to the db is covered, which should mean that the only error that can occur is a bad query e.g. selet * from table which would then (if debug is on) print out the bad query or (if debug is off) print out "There was an error accessing the database." (Summary: Its designed to catch a bad query, the connection/select problem is/should be covered with die(). This works so long the query to get the value of debug is correct.)

I include the file by using include(url); and it is placed at the top of another include file which assigns variables to be ready for when the next included template (tpl) file is parsed.

i.e.

/*
assign variables, process sessions, etc.
*/
include('http://www.thesite.com/includes/dbfunctions.php');
include('http://www.thesite.com/includes/utils.php');
include('http://www.thesite.com/includes/header.php');
/* etc. */


Additionally, the query works on the other pages so that is not the problem. It is complaining of a badly programmed method.

(Btw I appreciate the help!)can you please try the die I added just to see what it does?on monday, when i go back to work! ;)as predicted it provides exactly the same error.then you have issues someplace else if it doesn't even get that far to die. instead of using http, use the full path to include the files and what do you get?i have replacedinclude("http://www.thesite.com/includes/dbfunctions.php); with include("../includes/dbfunctions.php"); and it seems to work!

I find that very strange... I was under the impression that either way would yield the same location. Even stranger that it was just one particlular file that didnt work yet the others did...

Thanks for the suggestion Scoutt!
 
Back
Top