You are here: Re: Problem with require_once « PHP Programming Language « IT news, forums, messages
Re: Problem with require_once

Posted by Rik on 08/17/07 18:20

On Fri, 17 Aug 2007 20:10:53 +0200, Shelly =

<sheldonlg.news@asap-consult.com> wrote:
> 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" <luiheidsgoeroe@hotmail.com> wrote in message
> news:op.tw7o9owuqnv3q9@metallium...
> On Fri, 17 Aug 2007 18:14:03 +0200, Shelly
> <sheldonlg.news@asap-consult.com> wrote:
>> "Rik" <luiheidsgoeroe@hotmail.com> wrote in message
>> news:op.tw7mbyxuqnv3q9@metallium...
>> On Fri, 17 Aug 2007 17:13:19 +0200, Shelly
>> <sheldonlg.news@asap-consult.com> wrote:
>>> HOWEVER, If I comment out the require_once line and copy and paste t=
he
>>> code
>>> for it directly into the calling module, it works fine. Note that t=
his
>>> 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.
>>
>> =3D=3D=3D> No, the ONLY change between working and non-working versio=
ns 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 functio=
n
> where you pass it on as $con (or in the 'calling module' as you call i=
t),
> 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=3D "the_server_location";
> $database =3D "the_database_name";
> $dbUsername =3D "the_username";
> $dbPassword =3D "the_password";
> $dbCon =3D 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 loade=
d =

this file this entire request yet, _then_ load it, and exit the script i=
f =

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация