By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Warning: mysql_query(): 4 is not a valid MySQL-Link resource

P: n/a
Hi,

I got this warning:
mysql_query(): 4 is not a valid MySQL-Link resource.

The line which cause this warning is:
mysql_query("insert into $tablename (id,priority) values('$id',
'0.00')", $link2);

As far as understand the problem is that $link2 is "not a valid MySQL-
Link resource".
I specify $link2 in this way:
$link2 = mysql_connect( "localhost","loctopu_user","userpsw" );

And it seems to be valid to some place in my code (because I
successfully use it with different mysql_queries). The problem appears
after I call one function. After that $link2 is not a valid MySQL-Link
resource anymore. In the function I open and close another connection
to the MySQL server. But doing that I use another name for the link
resource. Moreover, I think, the link resource used in a function
should is treated a local variable, so it should be invisible from the
outside of the function.

So, what can it be? How a function can destroy the link resource of
the program which calls the function?

Thank you.
Feb 8 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Kurda Yon wrote:
Hi,

I got this warning:
mysql_query(): 4 is not a valid MySQL-Link resource.

The line which cause this warning is:
mysql_query("insert into $tablename (id,priority) values('$id',
'0.00')", $link2);

As far as understand the problem is that $link2 is "not a valid MySQL-
Link resource".
I specify $link2 in this way:
$link2 = mysql_connect( "localhost","loctopu_user","userpsw" );

And it seems to be valid to some place in my code (because I
successfully use it with different mysql_queries). The problem appears
after I call one function. After that $link2 is not a valid MySQL-Link
resource anymore. In the function I open and close another connection
to the MySQL server. But doing that I use another name for the link
resource. Moreover, I think, the link resource used in a function
should is treated a local variable, so it should be invisible from the
outside of the function.

So, what can it be? How a function can destroy the link resource of
the program which calls the function?

Thank you.
It's hard to tell without seeing the entire code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 8 '08 #2

P: n/a
On Feb 7, 10:04 pm, Kurda Yon <kurda...@yahoo.comwrote:
Hi,

I got this warning:
mysql_query(): 4 is not a valid MySQL-Link resource.

The line which cause this warning is:
mysql_query("insert into $tablename (id,priority) values('$id',
'0.00')", $link2);

As far as understand the problem is that $link2 is "not a valid MySQL-
Link resource".
I specify $link2 in this way:
$link2 = mysql_connect( "localhost","loctopu_user","userpsw" );

And it seems to be valid to some place in my code (because I
successfully use it with different mysql_queries). The problem appears
after I call one function. After that $link2 is not a valid MySQL-Link
resource anymore. In the function I open and close another connection
to the MySQL server. But doing that I use another name for the link
resource. Moreover, I think, the link resource used in a function
should is treated a local variable, so it should be invisible from the
outside of the function.

So, what can it be? How a function can destroy the link resource of
the program which calls the function?

Thank you.
In the manual under mysql_connect() is this note:

"If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned. The new_link parameter
modifies this behavior and makes mysql_connect() always open a new
link, even if mysql_connect() was called before with the same
parameters. In SQL safe mode, this parameter is ignored."

So, when you open the second link, you're really just getting a copy
of the first one. Thus, when you close it, you've closed the
connection in both places.

Some solutions:

1. Specify the new_link parameter when calling mysql_connect().
2. Don't explicitly close the connection -- you typically don't need
to anyway.
3. Use singletons.
Feb 8 '08 #3

P: n/a
ZeldorBlat wrote:
On Feb 7, 10:04 pm, Kurda Yon <kurda...@yahoo.comwrote:
>Hi,

I got this warning:
mysql_query(): 4 is not a valid MySQL-Link resource.

The line which cause this warning is:
mysql_query("insert into $tablename (id,priority) values('$id',
'0.00')", $link2);

As far as understand the problem is that $link2 is "not a valid MySQL-
Link resource".
I specify $link2 in this way:
$link2 = mysql_connect( "localhost","loctopu_user","userpsw" );

And it seems to be valid to some place in my code (because I
successfully use it with different mysql_queries). The problem appears
after I call one function. After that $link2 is not a valid MySQL-Link
resource anymore. In the function I open and close another connection
to the MySQL server. But doing that I use another name for the link
resource. Moreover, I think, the link resource used in a function
should is treated a local variable, so it should be invisible from the
outside of the function.

So, what can it be? How a function can destroy the link resource of
the program which calls the function?

Thank you.

In the manual under mysql_connect() is this note:

"If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned. The new_link parameter
modifies this behavior and makes mysql_connect() always open a new
link, even if mysql_connect() was called before with the same
parameters. In SQL safe mode, this parameter is ignored."

So, when you open the second link, you're really just getting a copy
of the first one. Thus, when you close it, you've closed the
connection in both places.
I saw that, but I'm not sure if that's what he's doing or not - hence
the request for the code.
Some solutions:

1. Specify the new_link parameter when calling mysql_connect().
True.
2. Don't explicitly close the connection -- you typically don't need
to anyway.
Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.
3. Use singletons.
The best way, IMHO.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 8 '08 #4

P: n/a
Greetings, Jerry Stuckle.
In reply to Your message dated Friday, February 8, 2008, 15:26:59,
>2. Don't explicitly close the connection -- you typically don't need
to anyway.
Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.
Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other scripts
using it.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 14 '08 #5

P: n/a
On Thu, 14 Feb 2008 21:27:56 +0100, AnrDaemon <an*******@freemail.ru>
wrote:
Greetings, Jerry Stuckle.
In reply to Your message dated Friday, February 8, 2008, 15:26:59,
>>2. Don't explicitly close the connection -- you typically don't need
to anyway.
>Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.

Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.
--
Rik Wasmus
Feb 14 '08 #6

P: n/a
AnrDaemon wrote:
Greetings, Jerry Stuckle.
In reply to Your message dated Friday, February 8, 2008, 15:26:59,
>>2. Don't explicitly close the connection -- you typically don't need
to anyway.
>Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.

Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other scripts
using it.

I always use PHP as a module.

But it's still a good idea to clean up after yourself. Don't depend on
the garbage collector to do it for you. It's sloppy programming, and
the mark of a lazy programmer.

For instance, it may be a while before the gc gets around to closing the
connection, especially on a busy system. The result is way more open
connections than you need.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 15 '08 #7

P: n/a
Greetings, Rik Wasmus.
In reply to Your message dated Thursday, February 14, 2008, 23:46:14,
>>>2. Don't explicitly close the connection -- you typically don't need
to anyway.
>>Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.

Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.
Sorry if I misunderstood developer's manual, but it said if I use persistent
connections to database (while using PHP as module, of course), database
connection will never be closed even if I explicitly call *_close, while there
are more than one script using this database connection.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 19 '08 #8

P: n/a
AnrDaemon wrote:
Greetings, Rik Wasmus.
In reply to Your message dated Thursday, February 14, 2008, 23:46:14,
>>>>2. Don't explicitly close the connection -- you typically don't need
to anyway.
Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.
Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
>If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.

Sorry if I misunderstood developer's manual, but it said if I use persistent
connections to database (while using PHP as module, of course), database
connection will never be closed even if I explicitly call *_close, while there
are more than one script using this database connection.

That's your first mistake. You shouldn't be using persistent
connections - they can decrease performance of your server.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 19 '08 #9

P: n/a
Greetings, Jerry Stuckle.
In reply to Your message dated Tuesday, February 19, 2008, 14:57:12,
>>>>>2. Don't explicitly close the connection -- you typically don't need
>to anyway.
Not true. You should always close connections when completely through
with them. It's good programming practice, if nothing else. But it
also means you're releasing system resources you no longer need, making
them available to others.
Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
>>If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.

Sorry if I misunderstood developer's manual, but it said if I use persistent
connections to database (while using PHP as module, of course), database
connection will never be closed even if I explicitly call *_close, while there
are more than one script using this database connection.
That's your first mistake. You shouldn't be using persistent
connections - they can decrease performance of your server.
It is not a mistake, it's made to work that way and there are no perfomance
issues with that. Please stop posting such comments.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 19 '08 #10

P: n/a
AnrDaemon wrote:
Greetings, Jerry Stuckle.
In reply to Your message dated Tuesday, February 19, 2008, 14:57:12,
>>>>>>2. Don't explicitly close the connection -- you typically don't need
>>to anyway.
>Not true. You should always close connections when completely through
>with them. It's good programming practice, if nothing else. But it
>also means you're releasing system resources you no longer need, making
>them available to others.
Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.
Sorry if I misunderstood developer's manual, but it said if I use persistent
connections to database (while using PHP as module, of course), database
connection will never be closed even if I explicitly call *_close, while there
are more than one script using this database connection.
>That's your first mistake. You shouldn't be using persistent
connections - they can decrease performance of your server.

It is not a mistake, it's made to work that way and there are no perfomance
issues with that. Please stop posting such comments.

You should really learn how persistent connections can hurt server
performance.

Please stop encouraging such poor programming techniques.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 19 '08 #11

P: n/a
AnrDaemon wrote:
Greetings, Jerry Stuckle.
In reply to Your message dated Tuesday, February 19, 2008, 14:57:12,
>>>>>>2. Don't explicitly close the connection -- you typically don't need
>>to anyway.
>Not true. You should always close connections when completely through
>with them. It's good programming practice, if nothing else. But it
>also means you're releasing system resources you no longer need, making
>them available to others.
Looks You have never used PHP as a module...
And database connection will be closed anyway when there are no other
scripts
using it.
If you don't, it will be open for the remainder of the script. On a high
traffic site, and a heavy page, hogging the connection for 0.5 seconds
while another script might need it is not a good thing... Maximum
connections to a database are usually limited.
Sorry if I misunderstood developer's manual, but it said if I use persistent
connections to database (while using PHP as module, of course), database
connection will never be closed even if I explicitly call *_close, while there
are more than one script using this database connection.
>That's your first mistake. You shouldn't be using persistent
connections - they can decrease performance of your server.

It is not a mistake, it's made to work that way and there are no perfomance
issues with that. Please stop posting such comments.

Oh, and one other thing - if you want to know why, ask in
comp.databases.mysql. They'll give you a lot of information.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 19 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.