|
Posted by Jerry Stuckle on 08/19/07 13:33
Shelly wrote:
> "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:
>>> 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.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|