|
Posted by Rik on 08/17/07 16:26
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:
>> I have an include file for the connection information to the server. =
It
>> is
>> like this:
>> $dbCon =3D 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 =3D "SELECT * FROM Company WHERE accountNumber=3D'" . $num . "'";=
>> $res =3D mysql_query($q, $con) or die(mysql_error());
>
> $con <> $dbCon
>
> =3D=3D=3D=3D> 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 th=
e
>> code
>> for it directly into the calling module, it works fine. Note that th=
is
>> 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 version=
s 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 becau=
se =
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
Navigation:
[Reply to this message]
|