473,785 Members | 3,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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("d bLogin.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 2075
Rik
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************ @asap-consult.comwrot e:
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_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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("d bLogin.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.comwro te in message
news:op.tw7mbyx uqnv3q9@metalli um...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************ @asap-consult.comwrot e:
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_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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("d bLogin.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.comwrot e:
"Rik" <lu************ @hotmail.comwro te in message
news:op.tw7mbyx uqnv3q9@metalli um...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************ @asap-consult.comwrot e:
>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_erro r());
mysql_select_d b($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("db Login.php").
The contents of dbLogin.php are:
<?php
$hostname= "the_server_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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.comwro te in message
news:op.tw7o9ow uqnv3q9@metalli um...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************ @asap-consult.comwrot e:
"Rik" <lu************ @hotmail.comwro te in message
news:op.tw7mbyx uqnv3q9@metalli um...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh************ @asap-consult.comwrot e:
>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_erro r());
mysql_select_d b($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.comwrot e:
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.comwro te in message
news:op.tw7o9ow uqnv3q9@metalli um...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************ @asap-consult.comwrot e:
>"Rik" <lu************ @hotmail.comwro te in message
news:op.tw7mby xuqnv3q9@metall ium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh*********** *@asap-consult.comwrot e:
>>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("db Login.php").
What if you dont _once() it as I asked?
The contents of dbLogin.php are:
<?php
$hostname= "the_server_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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.comwro te in message
news:op.tw7o9ow uqnv3q9@metalli um...
On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
<sh************ @asap-consult.comwrot e:
>"Rik" <lu************ @hotmail.comwro te in message
news:op.tw7mby xuqnv3q9@metall ium...
On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
<sh*********** *@asap-consult.comwrot e:
>>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_err or());
mysql_select_ db($database, $dbCon);

In my main file I have right near the top:

require_once( "dbLogin.ph p");

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("db Login.php").
The contents of dbLogin.php are:
<?php
$hostname= "the_server_loc ation";
$database = "the_database_n ame";
$dbUsername = "the_userna me";
$dbPassword = "the_passwo rd";
$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*******@attgl obal.net
=============== ===
Aug 19 '07 #7

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

Similar topics

6
38340
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 pages using the require_once function. That is, each page includes the line <?require_once('PageStart.php')?>
6
2725
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 directories from already included scripts... basically where there'a an include chain spanning multiple directories. I've reduced the problem to its core:
7
3326
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 require once within the included file. sample: fileA.php (in the root dir!) -----
0
1729
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 function below, look_at_data(). Line 4 "echo" below corrctly shows the var_name from code1.php. BUT, when I call it again at the end of the snippet below, it shows nothing.
2
1397
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 $favoriateColor='234566'; defined in KTConstants.php I tried include instead of require. I tried using $GLOBALS; perhaps I didn't do it right. The global keyword gives me an error...expected = or something.
6
2019
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 other <a hreflink on my page and user has to log into the system many times. How to build a page to have session variables values available on every page on my system
6
2498
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 |__/utils |__/logs/logger.inc When I run foo.php I get the following error:
2
2939
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 frame2.php (normally login / pass here but //removed unesecery code) just to show example
0
1573
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. unfortunately, no error message is given by the system. <?php class Mail_Tabelle extends Mail_DB { protected $description = false; const TBL_HEAD = ' <thead>
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.