473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
11 13156
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: photoelectric | last post by:
When trying to run the below script on an apache/1.3.26 with mysql and php I get the following error message: Warning: mysql_connect() : Access denied for user: 'nobody@software-ece.rutgers.edu'...
0
by: patel | last post by:
Hello, I get this warning with my PHP code sometimes. It will come up, and then after reloading the page once or twice, it will go away. Any ideas? The code is below: Warning: Unknown: Your...
0
by: dsclements | last post by:
>Description: I'm running mysql in a 3 server configuration, with 2 servers being slaves to the first. I'm running vpopmail, which means a connection every incoming mail and every check. I woke up...
5
by: Gabriel | last post by:
Hi, I have a problem with the c-api function mysql_query. When I try to send these query "SELECT a FROM b" the function mysql_query hang's up and the program stops at the codeline. If I kill...
5
by: Roman Mashak | last post by:
Hello, All! I already posted my question and received valuable feedbacks, I changed my code as was proposed here but still receive the same error of valgrind. SO, the code is: #define...
5
by: Chameleon | last post by:
I have a SQL Script with about 5000 SQL Commands. How can I send it to SQL Server at once? I see mysql_query() fails on first semicolon (;) who delimits the SQL Commands. Another...
3
by: smythe70 | last post by:
How do mysql_query() and mysql_fetch_assoc() actually work? When I submit a query to a MySQL database it goes away, works out what I requested and throws it back at me. When I submit a query...
3
by: gubbachchi | last post by:
Hi, I have written php code to retrive the image from mysql and display it on the webpage. Here is the code <?php require_once("DBConnect.php"); $gotten = @mysql_query("select user_photo from...
6
by: aerobat | last post by:
hello all just wondering if anyone can help me with this problem. Ii has to do with a simple mysql_query. i have tried so many different ways of doing it, but it wont work. here is the code: ...
11
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.