PHP mysql_close(): supplied argument is not a valid MySQL-Link resource 
Author Message
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource

Folks,
I have some functions written in PHP that perform connect, close, select,
append, delete routines... I can connect, perform my query fine, but when I
attempt to close the connection - it fails.

This is my 'connect' PHP code (which I know works)

    function MySQLconnect($MySQLdb)
    {

////////////////////////////////////////////////////////////////////////////
////
     // Function expects $MySQLdb to be an array with the following indexes
     // HOST, USER, PASSWORD
     // Returns with a link identifer to the database defined in
MySQLdb[LINK]
     //
     $link=mysql_pconnect("$MySQLdb[HOST]", "$MySQLdb[USER]",
"$MySQLdb[PASSWORD]");
     return($link);
    }

I call the above function, and connect to my database like so
    $link=MySQLconnect($MySQLdb);

I attempt to close the connection with
   mysql_close($link);

and then I get "Warning: mysql_close(): supplied argument is not a valid
MySQL-Link resource"

Where am I going wrong?  Is it wrong to pass the MySQ Link resource to/from
a function?  All help, via the newsgroup, would be much appreciated,

Regards
Randell D.



Tue, 08 Nov 2005 09:57:19 GMT  
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource
I'm guessing, but I think you many need to return a reference to $link

at the moment, your function is returning a copy of it, there's a
difference.

try

$link =& MySQLconnect($MySQLdb);

I'm not sure if you can declare a function like so

function &MySQLconnect($link,$MySQLdb)

if you can, then you won't have to remember to use a reference operator
everytime you want to use your function..

otherwise declar $link outside the function and pass it in by reference.
You function signature will look like this:
function MySQLconnect(&$link,$MySQLdb)

This is still pretty yucky. Of course if you were using OO, you would
just make $link a class attribute where all your member functions could
access it and you wouldn't have to pass around references (which goes
against the idea of encapsulation and scope anyway).

Better yet, do yourself a favour use PEAR DB for all your database
stuff. It eliminates these sorts of problems and offeres other benefits
such as not restricting your application to one type of DB server.

http://pear.php.net



Tue, 08 Nov 2005 14:01:19 GMT  
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource


Quote:
> I'm guessing, but I think you many need to return a reference to $link

> at the moment, your function is returning a copy of it, there's a
> difference.

> try

> $link =& MySQLconnect($MySQLdb);

> I'm not sure if you can declare a function like so

> function &MySQLconnect($link,$MySQLdb)

> if you can, then you won't have to remember to use a reference operator
> everytime you want to use your function..

> otherwise declar $link outside the function and pass it in by reference.
> You function signature will look like this:
> function MySQLconnect(&$link,$MySQLdb)

> This is still pretty yucky. Of course if you were using OO, you would
> just make $link a class attribute where all your member functions could
> access it and you wouldn't have to pass around references (which goes
> against the idea of encapsulation and scope anyway).

> Better yet, do yourself a favour use PEAR DB for all your database
> stuff. It eliminates these sorts of problems and offeres other benefits
> such as not restricting your application to one type of DB server.

> http://pear.php.net

Your first suggestion sounds plausable and I'll check in to it...

Classes though? I somehow have problems understanding classes - I know its a
big failure in my PHP skillset but I just can't get my head around it... so
part of your solution about is abit above my head... on the other side
though, I'll check in to the PEAR DB that you reference...

Thanks for the prompt reply...
randelld



Tue, 08 Nov 2005 14:11:49 GMT  
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource
I use a similar function without any problems, but my calling sequence is
slightly different:

$dbconnect = NULL;

$dbhost = "localhost";

$dbname = "blah";

$dbusername = "blah";

$dbuserpass = "blah";

$query = NULL;

function db_connect($dbname)

{

    global $dbconnect, $dbhost, $dbusername, $dbuserpass;

   if (!$dbconnect) $dbconnect = mysql_connect($dbhost, $dbusername,
$dbuserpass);

    if (!$dbconnect) {

        return 0;

    } elseif (!mysql_select_db($dbname)) {

        return 0;

    } else {

        return $dbconnect;

    } // if

Quote:
} // db_connect

$db = db_connect($dbname) or trigger_error("SQL", E_USER_ERROR);

This works for me.

--
Tony Marston




http://www.tonymarston.net


Quote:
> Folks,
> I have some functions written in PHP that perform connect, close, select,
> append, delete routines... I can connect, perform my query fine, but when
I
> attempt to close the connection - it fails.

> This is my 'connect' PHP code (which I know works)

>     function MySQLconnect($MySQLdb)
>     {

////////////////////////////////////////////////////////////////////////////
Quote:
> ////
>      // Function expects $MySQLdb to be an array with the following
indexes
>      // HOST, USER, PASSWORD
>      // Returns with a link identifer to the database defined in
> MySQLdb[LINK]
>      //
>      $link=mysql_pconnect("$MySQLdb[HOST]", "$MySQLdb[USER]",
> "$MySQLdb[PASSWORD]");
>      return($link);
>     }

> I call the above function, and connect to my database like so
>     $link=MySQLconnect($MySQLdb);

> I attempt to close the connection with
>    mysql_close($link);

> and then I get "Warning: mysql_close(): supplied argument is not a valid
> MySQL-Link resource"

> Where am I going wrong?  Is it wrong to pass the MySQ Link resource
to/from
> a function?  All help, via the newsgroup, would be much appreciated,

> Regards
> Randell D.



Tue, 08 Nov 2005 17:33:25 GMT  
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource


Quote:
> Folks,
> I have some functions written in PHP that perform connect, close, select,
> append, delete routines... I can connect, perform my query fine, but when
I
> attempt to close the connection - it fails.

> This is my 'connect' PHP code (which I know works)

>     function MySQLconnect($MySQLdb)
>     {

////////////////////////////////////////////////////////////////////////////

Quote:
> ////
>      // Function expects $MySQLdb to be an array with the following
indexes
>      // HOST, USER, PASSWORD
>      // Returns with a link identifer to the database defined in
> MySQLdb[LINK]
>      //
>      $link=mysql_pconnect("$MySQLdb[HOST]", "$MySQLdb[USER]",
> "$MySQLdb[PASSWORD]");
>      return($link);
>     }

> I call the above function, and connect to my database like so
>     $link=MySQLconnect($MySQLdb);

> I attempt to close the connection with
>    mysql_close($link);

> and then I get "Warning: mysql_close(): supplied argument is not a valid
> MySQL-Link resource"

> Where am I going wrong?  Is it wrong to pass the MySQ Link resource
to/from
> a function?  All help, via the newsgroup, would be much appreciated,

> Regards
> Randell D.

The reason why I wanted to close my connections were because I was finding
alot of open connections to the database through my own testing... Everytime
my scripts accessed the database, a new connection was opened and my
understanding with mysql_pconnect was that they were persistent connections
meaning before a new one was opened, a check was done to see if there were
an already existing/free link available.

On my Linux and Windoze development environments neither of these proved
true - ie Within a minute or so of testing my application through my web
browser I would end up with several sleeping processes in use in the
database... In some cases, I ended up with as many as sixty or so
connections to the database after only a few minutes of testing - surely
this is not right? Surely this would eventually effect the performance of my
server, true?

Have I discovered a bug? I'm using PHP 4.3.1 with Apache 1.3.27 and MySQL
3.23.56

The piece of text below comes from the PHP.net windoze help file

"mysql_pconnect() acts very much like mysql_connect() with two major
differences.
- First, when connecting, the function would first try to find a
(persistent) link that's already open with the same host, username and
password. If one is found, an identifier for it will be returned instead of
opening a new connection.
- Second, the connection to the SQL server will not be closed when the
execution of the script ends. Instead, the link will remain open for future
use (mysql_close() will not close links established by mysql_pconnect())."

I'm afraid to get to far down in to my project only to discover that its an
error on my part as opposed to a bug in the PHP/MySQL implementation that
I'm using...



Wed, 09 Nov 2005 03:27:52 GMT  
 PHP mysql_close(): supplied argument is not a valid MySQL-Link resource

Quote:



>>Folks,
>>I have some functions written in PHP that perform connect, close, select,
>>append, delete routines... I can connect, perform my query fine, but when

> I

>>attempt to close the connection - it fails.

>>This is my 'connect' PHP code (which I know works)

>>    function MySQLconnect($MySQLdb)
>>    {

> ////////////////////////////////////////////////////////////////////////////

>>////
>>     // Function expects $MySQLdb to be an array with the following

> indexes

>>     // HOST, USER, PASSWORD
>>     // Returns with a link identifer to the database defined in
>>MySQLdb[LINK]
>>     //
>>     $link=mysql_pconnect("$MySQLdb[HOST]", "$MySQLdb[USER]",
>>"$MySQLdb[PASSWORD]");
>>     return($link);
>>    }

>>I call the above function, and connect to my database like so
>>    $link=MySQLconnect($MySQLdb);

>>I attempt to close the connection with
>>   mysql_close($link);

>>and then I get "Warning: mysql_close(): supplied argument is not a valid
>>MySQL-Link resource"

>>Where am I going wrong?  Is it wrong to pass the MySQ Link resource

> to/from

>>a function?  All help, via the newsgroup, would be much appreciated,

>>Regards
>>Randell D.

> The reason why I wanted to close my connections were because I was finding
> alot of open connections to the database through my own testing... Everytime
> my scripts accessed the database, a new connection was opened and my
> understanding with mysql_pconnect was that they were persistent connections
> meaning before a new one was opened, a check was done to see if there were
> an already existing/free link available.

> On my Linux and Windoze development environments neither of these proved
> true - ie Within a minute or so of testing my application through my web
> browser I would end up with several sleeping processes in use in the
> database... In some cases, I ended up with as many as sixty or so
> connections to the database after only a few minutes of testing - surely
> this is not right? Surely this would eventually effect the performance of my
> server, true?

> Have I discovered a bug? I'm using PHP 4.3.1 with Apache 1.3.27 and MySQL
> 3.23.56

> The piece of text below comes from the PHP.net windoze help file

> "mysql_pconnect() acts very much like mysql_connect() with two major
> differences.
> - First, when connecting, the function would first try to find a
> (persistent) link that's already open with the same host, username and
> password. If one is found, an identifier for it will be returned instead of
> opening a new connection.
> - Second, the connection to the SQL server will not be closed when the
> execution of the script ends. Instead, the link will remain open for future
> use (mysql_close() will not close links established by mysql_pconnect())."

> I'm afraid to get to far down in to my project only to discover that its an
> error on my part as opposed to a bug in the PHP/MySQL implementation that
> I'm using...

use PEAR DB and then test for{*filter*} connections again.

(to use anything from PEAR, you need to understand OO to some degree)



Fri, 11 Nov 2005 08:59:05 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. mysql_query(): supplied argument is not a valid MySQL-Link

2. supplied argument is not a valid File-Handle resource

3. mysql_fetch_row(): supplied argument is not a valid

4. MySQL resource link going bad on me

5. mysql is not working with php

6. PHP frontend to MySQL (not phpMyAdmin)

7. MySQL query in PHP: Not query zero amounts also removes null amounts from query

8. PHP/mySQL not displaying anything...???

9. PHP not inserting data correctly into MySQL

10. php mysql sample code php shareware like KB knowledgebase

11. PHP+MySQL vs. PHP+PostgreSQL

12. php + mysql or php + postgresql?

 

 
Powered by phpBB® Forum Software