473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Problem with require_once

Here is a crazy question that has happend to me once before.

I have an include file for the connection information to the server. It is
like this:

$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The quotes
are single-double for the fist one and double-single-double for the second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());

This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the code
for it directly into the calling module, it works fine. Note that this was
a strict cut and paste and the only keyboard strokes that I did were the the
slashes to comment out the require_once.

Does anyone have any ideas?

Shelly
Aug 17 '07 #1
6 2052
Rik
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
Here is a crazy question that has happend to me once before.

I have an include file for the connection information to the server. It
is
like this:

$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
Are you very sure you need mysql_pconnect()? If you do not know exactly
what it does are you are unsure, you clearly need mysql_connect().
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The
quotes
are single-double for the fist one and double-single-double for the
second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());
$con <$dbCon

$res = mysql_query($q, $dbCon) or die(mysql_error());
This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were the
the
slashes to comment out the require_once.
Hmmmz, I think the fault is elsewhere.
--
Rik Wasmus
Aug 17 '07 #2

"Rik" <lu************@hotmail.comwrote in message
news:op.tw7mbyxuqnv3q9@metallium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
Here is a crazy question that has happend to me once before.

I have an include file for the connection information to the server. It
is
like this:

$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
Are you very sure you need mysql_pconnect()? If you do not know exactly
what it does are you are unsure, you clearly need mysql_connect().

=== Yes, I can use mysql_connect. I don't need a persistent connection.
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The
quotes
are single-double for the fist one and double-single-double for the
second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());
$con <$dbCon

==== My typo threw you off. I pass in $dbCon as $con, so I use $con.
When the code is inline it works. It doesn't when I have the require_once.

$res = mysql_query($q, $dbCon) or die(mysql_error());
This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were the
the
slashes to comment out the require_once.
Hmmmz, I think the fault is elsewhere.

===No, the ONLY change between working and non-working versions is that I
paste the lines on code in for the working version and use a require_once in
the non-working version.
--
Rik Wasmus
Aug 17 '07 #3
Rik
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************@asap-consult.comwrote:
"Rik" <lu************@hotmail.comwrote in message
news:op.tw7mbyxuqnv3q9@metallium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
>I have an include file for the connection information to the server. It
is
like this:
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The
quotes
are single-double for the fist one and double-single-double for the
second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());

$con <$dbCon

==== My typo threw you off. I pass in $dbCon as $con, so I use $con.
When the code is inline it works.
OK
>This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were the
the
slashes to comment out the require_once.

Hmmmz, I think the fault is elsewhere.

===No, the ONLY change between working and non-working versions is
that I
paste the lines on code in for the working version and use a
require_once in
the non-working version.
Then I guess it's a scope issue: $dbCon is not in scope in the function
where you pass it on as $con (or in the 'calling module' as you call it),
so it will be NULL. (Enable error_reporting(E_ALL | E_STRICT); to get a
notice at that point). Either because it's called out of scope, or because
is has been required earlier in the code, so it will not be required()
again (hence the _once). Require_once is good for class definitions,
defines, and functions, not for variables or resources. You could do
something like this in the dbLogin.php:

global $dbCon;
if(!is_resource($dbCon)){
//connect to db
}

And then just require(), not require_once().

If that's not the case either, care to give the actual files (without
passwords. etc offcourse) instead of snippets?
--
Rik Wasmus
Aug 17 '07 #4
I guess I am not making myself clear to you Rik. Let me try again.

In the calling file I have a function into which I pass $dbCon as $con. I
then make my database calls. There are two cases.

Case 1: At the beginning of the module I have a require_one("dbLogin.php").
The contents of dbLogin.php are:
<?php
$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
?>
This gives me the error of invalid resource.

Case 2: At the beginning of the module I paste in the contents of
dbLogin.php and comment out the require_once statement. (The contents are
the lines from $hostname to $dbCon).
This works.

Both of these use a call to a function in the module, so it can't be a
matter of scope. It is simply the difference between including the contents
inline or including it via a named file.

Shelly
"Rik" <lu************@hotmail.comwrote in message
news:op.tw7o9owuqnv3q9@metallium...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************@asap-consult.comwrote:
"Rik" <lu************@hotmail.comwrote in message
news:op.tw7mbyxuqnv3q9@metallium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
>I have an include file for the connection information to the server. It
is
like this:
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The
quotes
are single-double for the fist one and double-single-double for the
second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());

$con <$dbCon

==== My typo threw you off. I pass in $dbCon as $con, so I use $con.
When the code is inline it works.
OK
>This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were the
the
slashes to comment out the require_once.

Hmmmz, I think the fault is elsewhere.

===No, the ONLY change between working and non-working versions is that
I
paste the lines on code in for the working version and use a require_once
in
the non-working version.
Then I guess it's a scope issue: $dbCon is not in scope in the function
where you pass it on as $con (or in the 'calling module' as you call it),
so it will be NULL. (Enable error_reporting(E_ALL | E_STRICT); to get a
notice at that point). Either because it's called out of scope, or because
is has been required earlier in the code, so it will not be required()
again (hence the _once). Require_once is good for class definitions,
defines, and functions, not for variables or resources. You could do
something like this in the dbLogin.php:

global $dbCon;
if(!is_resource($dbCon)){
//connect to db
}

And then just require(), not require_once().

If that's not the case either, care to give the actual files (without
passwords. etc offcourse) instead of snippets?
--
Rik Wasmus
Aug 17 '07 #5
Rik
On Fri, 17 Aug 2007 20:10:53 +0200, Shelly
<sh************@asap-consult.comwrote:
I guess I am not making myself clear to you Rik. Let me try again.
Please don't toppost, fixed. No easy way to fix the quoting though
"Rik" <lu************@hotmail.comwrote in message
news:op.tw7o9owuqnv3q9@metallium...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************@asap-consult.comwrote:
>"Rik" <lu************@hotmail.comwrote in message
news:op.tw7mbyxuqnv3q9@metallium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
>>HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were
the
the
slashes to comment out the require_once.

Hmmmz, I think the fault is elsewhere.

===No, the ONLY change between working and non-working versions is
that
I
paste the lines on code in for the working version and use a
require_once
in
the non-working version.

Then I guess it's a scope issue: $dbCon is not in scope in the function
where you pass it on as $con (or in the 'calling module' as you call it),
so it will be NULL. (Enable error_reporting(E_ALL | E_STRICT); to get a
notice at that point). Either because it's called out of scope, or
because
is has been required earlier in the code, so it will not be required()
again (hence the _once). Require_once is good for class definitions,
defines, and functions, not for variables or resources. You could do
something like this in the dbLogin.php:

global $dbCon;
if(!is_resource($dbCon)){
//connect to db
}

And then just require(), not require_once().
In the calling file I have a function into which I pass $dbCon as $con..
I
then make my database calls. There are two cases.

Case 1: At the beginning of the module
Define 'module': OO or just straight PHP?
I have a require_one("dbLogin.php").
What if you dont _once() it as I asked?
The contents of dbLogin.php are:
<?php
$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
?>
This gives me the error of invalid resource.

Case 2: At the beginning of the module I paste in the contents of
dbLogin.php and comment out the require_once statement. (The contents
are
the lines from $hostname to $dbCon).
This works.

Both of these use a call to a function in the module, so it can't be a
matter of scope. It is simply the difference between including the
contents
inline or including it via a named file.
You don't simply 'include' , require_once is saying 'If I have not loaded
this file this entire request yet, _then_ load it, and exit the script if
it doesn't work'.
>If that's not the case either, care to give the actual files (without
passwords. etc offcourse) instead of snippets?
If you are damned sure the require_once() statement you use is the firsy&
only require()/include()ing of the database connection file, so it's
called nowhere else before it, as said, I'd like to see the whole file.
--
Rik Wasmus
Aug 17 '07 #6
Shelly wrote:
"Rik" <lu************@hotmail.comwrote in message
news:op.tw7o9owuqnv3q9@metallium...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************@asap-consult.comwrote:
>"Rik" <lu************@hotmail.comwrote in message
news:op.tw7mbyxuqnv3q9@metallium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************@asap-consult.comwrote:
>>I have an include file for the connection information to the server. It
is
like this:
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
mysql_select_db($database, $dbCon);

In my main file I have right near the top:

require_once("dbLogin.php");

I then have lines in a function where I pass in $sbCon as $con (The
quotes
are single-double for the fist one and double-single-double for the
second
one):

$q = "SELECT * FROM Company WHERE accountNumber='" . $num . "'";
$res = mysql_query($q, $con) or die(mysql_error());
$con <$dbCon

==== My typo threw you off. I pass in $dbCon as $con, so I use $con.
When the code is inline it works.

OK
>>This gives me an invalid resource at the mysql_query line.

HOWEVER, If I comment out the require_once line and copy and paste the
code
for it directly into the calling module, it works fine. Note that this
was
a strict cut and paste and the only keyboard strokes that I did were the
the
slashes to comment out the require_once.
Hmmmz, I think the fault is elsewhere.

===No, the ONLY change between working and non-working versions is that
I
paste the lines on code in for the working version and use a require_once
in
the non-working version.

Then I guess it's a scope issue: $dbCon is not in scope in the function
where you pass it on as $con (or in the 'calling module' as you call it),
so it will be NULL. (Enable error_reporting(E_ALL | E_STRICT); to get a
notice at that point). Either because it's called out of scope, or because
is has been required earlier in the code, so it will not be required()
again (hence the _once). Require_once is good for class definitions,
defines, and functions, not for variables or resources. You could do
something like this in the dbLogin.php:

global $dbCon;
if(!is_resource($dbCon)){
//connect to db
}

And then just require(), not require_once().

If that's not the case either, care to give the actual files (without
passwords. etc offcourse) instead of snippets?
I guess I am not making myself clear to you Rik. Let me try again.

In the calling file I have a function into which I pass $dbCon as
$con. I
then make my database calls. There are two cases.

Case 1: At the beginning of the module I have a
require_one("dbLogin.php").
The contents of dbLogin.php are:
<?php
$hostname= "the_server_location";
$database = "the_database_name";
$dbUsername = "the_username";
$dbPassword = "the_password";
$dbCon = mysql_pconnect($hostname, $dbUsername, $dbPassword) or
die(mysql_error());
?>
This gives me the error of invalid resource.

Case 2: At the beginning of the module I paste in the contents of
dbLogin.php and comment out the require_once statement. (The
contents are
the lines from $hostname to $dbCon).
This works.

Both of these use a call to a function in the module, so it can't be a
matter of scope. It is simply the difference between including the
contents
inline or including it via a named file.

Shelly

(Top posting fixed)

Are you sure you don't have two files named dblogin.php on your system?
Perhaps you're picking up the wrong one?

Otherwise, I agree with Rik - please paste the entire code from both
files so we can get a better idea what's going on. Because the way you
explain it shouldn't occur, so there's obviously some important point
missing.

P.S. Please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: the wonderer | last post by:
This is an elementary question, but I've not been able to find the answer, so here goes: I am developing a site using php. I have the html header information in a file that I include in all the...
6
by: Rob Long | last post by:
Hey, I've written a custom HTML library using many PHP scripts as seperate files (just like I'd do a java project) and I'm having some problems where I'm including scripts in different...
7
by: Lars Plessmann | last post by:
I hava several php files in different folders. I tried to use relative paths. But when I call a script from another folder that has another depth, it cannot find the files that are embedded via...
0
by: NotGiven | last post by:
Below is a code snippet I am having a hard time with. I rerquire code1.php and code 2.php. code1.php contains some variables the code needs. code2.php contains some functions I call from the...
2
by: J | last post by:
I have a page search.php which uses Top.php and Bottom.php to do most webpage stuff common to all pages. Top.php includes/requires KTConstants.php. Top.php can't see Constants/variables like...
6
by: K. | last post by:
Hello!!!! I have a problems with sessions in mojavi technology. I have created a login panel to log into the system: Unfortunately sessions variable disappears while trying to click on any...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
2
by: panos100m | last post by:
Hi there I am using a menu frame on the left side but i cant destroy it using the logout link that i created ... any ideas? //index.php // PAGE this will just redirect to the...
0
Dormilich
by: Dormilich | last post by:
this is a follow-up thread to this one. http://bytes.com/topic/html-css/answers/863662-form-not-submitted-sometimes I figured out that the mail sending class triggers the described error....
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.