not a valid MySQL-Link resource

This is a discussion on not a valid MySQL-Link resource within the PHP Language forums, part of the PHP Programming Forums category; Hi everybody, I cannot understand the following thinks. The last line of the fillowing code produces a message about mistake (...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-26-2007
Kurda Yon
 
Posts: n/a
Default not a valid MySQL-Link resource

Hi everybody,

I cannot understand the following thinks. The last line of the
fillowing code produces a message about mistake (not a valid MySQL-
Link resource):

$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_select_db( $db_name, $link);
$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_close( $link );
mysql_select_db( "sss", $link );

It is clear why it is happens. Because I use $link to the database,
which has been closed in the previouse line. To remove this problem I
have to change the code in the following way:

$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_select_db( $db_name, $link);
$link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_close( $link_new );
mysql_select_db( "sss", $link );

Now I do the same but with the usage of a function. As it is expected,
the following code produce the message about mistake:

$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_select_db( $db_name, $link);
function fff()
{
$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_close( $link );
}
fff();
mysql_select_db( "sss", $link );

But what is strange and what I cannot understand is why the previosly
used solution does not work. The following code alse generate the same
message!

$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_select_db( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_close( $link_new );
}
fff();
mysql_select_db( "sss", $link );

Thanks.

Reply With Quote
  #2 (permalink)  
Old 04-26-2007
Steve
 
Posts: n/a
Default Re: not a valid MySQL-Link resource


"Kurda Yon" <kurdayon@yahoo.com> wrote in message
news:1177613411.140647.31800@n35g2000prd.googlegro ups.com...
| Hi everybody,
|
| I cannot understand the following thinks. The last line of the
| fillowing code produces a message about mistake (not a valid MySQL-
| Link resource):
|
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_select_db( $db_name, $link);
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_close( $link );
| mysql_select_db( "sss", $link );
|
| It is clear why it is happens. Because I use $link to the database,
| which has been closed in the previouse line. To remove this problem I
| have to change the code in the following way:
|
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_select_db( $db_name, $link);
| $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_close( $link_new );
| mysql_select_db( "sss", $link );
|
| Now I do the same but with the usage of a function. As it is expected,
| the following code produce the message about mistake:
|
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_select_db( $db_name, $link);
| function fff()
| {
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_close( $link );
| }
| fff();
| mysql_select_db( "sss", $link );
|
| But what is strange and what I cannot understand is why the previosly
| used solution does not work. The following code alse generate the same
| message!
|
| $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_select_db( $db_name, $link);
| function fff()
| {
| $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
| mysql_close( $link_new );
| }
| fff();
| mysql_select_db( "sss", $link );

hard to understand you.

is this your *real* code? you realize that $link INSIDE the function is not
$link OUTSIDE your function, right? why are you worrying about closing the
connection anyway? the connection is dropped after the script is run/exited.


Reply With Quote
  #3 (permalink)  
Old 04-27-2007
Kurda Yon
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

On Apr 26, 9:20 pm, "Steve" <no....@example.com> wrote:
> "Kurda Yon" <kurda...@yahoo.com> wrote in message
>
> news:1177613411.140647.31800@n35g2000prd.googlegro ups.com...
> | Hi everybody,
> |
> | I cannot understand the following thinks. The last line of the
> | fillowing code produces a message about mistake (not a valid MySQL-
> | Link resource):
> |
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_select_db( $db_name, $link);
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_close( $link );
> | mysql_select_db( "sss", $link );
> |
> | It is clear why it is happens. Because I use $link to the database,
> | which has been closed in the previouse line. To remove this problem I
> | have to change the code in the following way:
> |
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_select_db( $db_name, $link);
> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_close( $link_new );
> | mysql_select_db( "sss", $link );
> |
> | Now I do the same but with the usage of a function. As it is expected,
> | the following code produce the message about mistake:
> |
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_select_db( $db_name, $link);
> | function fff()
> | {
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_close( $link );
> | }
> | fff();
> | mysql_select_db( "sss", $link );
> |
> | But what is strange and what I cannot understand is why the previosly
> | used solution does not work. The following code alse generate the same
> | message!
> |
> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_select_db( $db_name, $link);
> | function fff()
> | {
> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> | mysql_close( $link_new );
> | }
> | fff();
> | mysql_select_db( "sss", $link );
>
> hard to understand you.
>
> is this your *real* code? you realize that $link INSIDE the function is not
> $link OUTSIDE your function, right? why are you worrying about closing the
> connection anyway? the connection is dropped after the script is run/exited.


Sorry, may be I explained it in a bad way. The problem is that the
following code produce a message about mistake:

$link = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_select_db( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
mysql_close( $link_new );
}
fff();
mysql_select_db( "sss", $link );

Or more precisely the last line causes the following message:
Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource

And it happens only if I execute the fff() function before. I suppose
that origin of the problem is that I close connection to the database
in the function. But I do not understand why. I do give another name
to the connection.


Reply With Quote
  #4 (permalink)  
Old 04-27-2007
Schraalhans Keukenmeester
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

Kurda Yon wrote:
> On Apr 26, 9:20 pm, "Steve" <no....@example.com> wrote:
>> "Kurda Yon" <kurda...@yahoo.com> wrote in message
>>
>> news:1177613411.140647.31800@n35g2000prd.googlegro ups.com...
>> | Hi everybody,
>> |
>> | I cannot understand the following thinks. The last line of the
>> | fillowing code produces a message about mistake (not a valid MySQL-
>> | Link resource):
>> |
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_select_db( $db_name, $link);
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_close( $link );
>> | mysql_select_db( "sss", $link );
>> |
>> | It is clear why it is happens. Because I use $link to the database,
>> | which has been closed in the previouse line. To remove this problem I
>> | have to change the code in the following way:
>> |
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_select_db( $db_name, $link);
>> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_close( $link_new );
>> | mysql_select_db( "sss", $link );
>> |
>> | Now I do the same but with the usage of a function. As it is expected,
>> | the following code produce the message about mistake:
>> |
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_select_db( $db_name, $link);
>> | function fff()
>> | {
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_close( $link );
>> | }
>> | fff();
>> | mysql_select_db( "sss", $link );
>> |
>> | But what is strange and what I cannot understand is why the previosly
>> | used solution does not work. The following code alse generate the same
>> | message!
>> |
>> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_select_db( $db_name, $link);
>> | function fff()
>> | {
>> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
>> | mysql_close( $link_new );
>> | }
>> | fff();
>> | mysql_select_db( "sss", $link );
>>
>> hard to understand you.
>>
>> is this your *real* code? you realize that $link INSIDE the function is not
>> $link OUTSIDE your function, right? why are you worrying about closing the
>> connection anyway? the connection is dropped after the script is run/exited.

>
> Sorry, may be I explained it in a bad way. The problem is that the
> following code produce a message about mistake:
>
> $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> mysql_select_db( $db_name, $link);
> function fff()
> {
> $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> mysql_close( $link_new );
> }
> fff();
> mysql_select_db( "sss", $link );
>
> Or more precisely the last line causes the following message:
> Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource
>
> And it happens only if I execute the fff() function before. I suppose
> that origin of the problem is that I close connection to the database
> in the function. But I do not understand why. I do give another name
> to the connection.
>
>

From the mysql_connect() manual page:

resource mysql_connect ( [string $server [, string $username [, string
$password [, bool $new_link [, int $client_flags]]]]] )

[...]

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.

This might be what causes your error.

HTH

Sh.
Reply With Quote
  #5 (permalink)  
Old 04-27-2007
Kurda Yon
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

On Apr 27, 12:07 pm, Schraalhans Keukenmeester
<bitbuc...@invalid.spam> wrote:
> Kurda Yon wrote:
> > On Apr 26, 9:20 pm, "Steve" <no....@example.com> wrote:
> >> "Kurda Yon" <kurda...@yahoo.com> wrote in message

>
> >>news:1177613411.140647.31800@n35g2000prd.googleg roups.com...
> >> | Hi everybody,
> >> |
> >> | I cannot understand the following thinks. The last line of the
> >> | fillowing code produces a message about mistake (not a valid MySQL-
> >> | Link resource):
> >> |
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_select_db( $db_name, $link);
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_close( $link );
> >> | mysql_select_db( "sss", $link );
> >> |
> >> | It is clear why it is happens. Because I use $link to the database,
> >> | which has been closed in the previouse line. To remove this problem I
> >> | have to change the code in the following way:
> >> |
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_select_db( $db_name, $link);
> >> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_close( $link_new );
> >> | mysql_select_db( "sss", $link );
> >> |
> >> | Now I do the same but with the usage of a function. As it is expected,
> >> | the following code produce the message about mistake:
> >> |
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_select_db( $db_name, $link);
> >> | function fff()
> >> | {
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_close( $link );
> >> | }
> >> | fff();
> >> | mysql_select_db( "sss", $link );
> >> |
> >> | But what is strange and what I cannot understand is why the previosly
> >> | used solution does not work. The following code alse generate the same
> >> | message!
> >> |
> >> | $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_select_db( $db_name, $link);
> >> | function fff()
> >> | {
> >> | $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> >> | mysql_close( $link_new );
> >> | }
> >> | fff();
> >> | mysql_select_db( "sss", $link );

>
> >> hard to understand you.

>
> >> is this your *real* code? you realize that $link INSIDE the function is not
> >> $link OUTSIDE your function, right? why are you worrying about closing the
> >> connection anyway? the connection is dropped after the script is run/exited.

>
> > Sorry, may be I explained it in a bad way. The problem is that the
> > following code produce a message about mistake:

>
> > $link = mysql_connect( "localhost","tmp_user","tmpxxx" );
> > mysql_select_db( $db_name, $link);
> > function fff()
> > {
> > $link_new = mysql_connect( "localhost","tmp_user","tmpxxx" );
> > mysql_close( $link_new );
> > }
> > fff();
> > mysql_select_db( "sss", $link );

>
> > Or more precisely the last line causes the following message:
> > Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource

>
> > And it happens only if I execute the fff() function before. I suppose
> > that origin of the problem is that I close connection to the database
> > in the function. But I do not understand why. I do give another name
> > to the connection.

>
> From the mysql_connect() manual page:
>
> resource mysql_connect ( [string $server [, string $username [, string
> $password [, bool $new_link [, int $client_flags]]]]] )
>
> [...]
>
> 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.
>
> This might be what causes your error.
>
> HTH
>
> Sh.


Thanks! It helps.

Reply With Quote
  #6 (permalink)  
Old 04-27-2007
Kurda Yon
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

It is me again. It seems that I solve the previous problem (with your
help) but get a new one (which seems to be very related to the
previous one).

This part of code produce a message about mistake:

$link = mysql_connect( "localhost","tmp_user","pswxxx" );
function fff()
{
$link_new = mysql_connect( "localhost","tmp_user","pswxxx", true );
mysql_close( $link_new );
}
fff();
mysql_select_db( "sss", $link );
$result = mysql_query( "select * from root" );

The message is:
Access denied for user: 'nobody@localhost' (Using password: NO)
A link to the server could not be established

It complains about the last line. And this message disappears if I do
not coll the function fff (I replace fff(); by //fff();).

Can anybody explain this behavior? It seems that connection made to
the database in the function somehow influences on other connection.

Reply With Quote
  #7 (permalink)  
Old 04-27-2007
Schraalhans Keukenmeester
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

On Fri, 27 Apr 2007 04:53:04 -0700, Kurda Yon wrote:

> It is me again. It seems that I solve the previous problem (with your
> help) but get a new one (which seems to be very related to the
> previous one).
>
> This part of code produce a message about mistake:
>
> $link = mysql_connect( "localhost","tmp_user","pswxxx" );
> function fff()
> {
> $link_new = mysql_connect( "localhost","tmp_user","pswxxx", true );
> mysql_close( $link_new );
> }
> fff();
> mysql_select_db( "sss", $link );
> $result = mysql_query( "select * from root" );
>
> The message is:
> Access denied for user: 'nobody@localhost' (Using password: NO)
> A link to the server could not be established
>
> It complains about the last line. And this message disappears if I do
> not coll the function fff (I replace fff(); by //fff();).
>
> Can anybody explain this behavior? It seems that connection made to
> the database in the function somehow influences on other connection.


I'm guessing here: You use literal strings here in your post making the
mysql connection, but do you perhaps use variables in your real code? If
so, (and assuming globals are off) the function doesn't see these
variables unless you specify them global in the function.

Other than that, I can't think of anything right now. I cannot reproduce
the error anyway with my code (below).

<?PHP
require 'dbdata.inc.php';
$link = mysql_connect($dbhost,$dbuser,$dbpasswd);
function dbopen() {
global $dbhost, $dbuser, $dbpasswd;
$new_link = mysql_connect($dbhost,$dbuser,$dbpasswd,true);
mysql_close($new_link);
}
dbopen();
mysql_select_db($dbname,$link);
$result= mysql_query('select count(*) from customers',$link);
$row = mysql_fetch_row($result);
echo $row['0']; // outputs 13, oughtta be closer to 50 if you ask me;-)
?>

Reply With Quote
  #8 (permalink)  
Old 04-27-2007
Kurda Yon
 
Posts: n/a
Default Re: not a valid MySQL-Link resource

On Apr 27, 3:02 pm, Schraalhans Keukenmeester <inva...@invalid.spam>
wrote:
> On Fri, 27 Apr 2007 04:53:04 -0700, Kurda Yon wrote:
> > It is me again. It seems that I solve the previous problem (with your
> > help) but get a new one (which seems to be very related to the
> > previous one).

>
> > This part of code produce a message about mistake:

>
> > $link = mysql_connect( "localhost","tmp_user","pswxxx" );
> > function fff()
> > {
> > $link_new = mysql_connect( "localhost","tmp_user","pswxxx", true );
> > mysql_close( $link_new );
> > }
> > fff();
> > mysql_select_db( "sss", $link );
> > $result = mysql_query( "select * from root" );

>
> > The message is:
> > Access denied for user: 'nobody@localhost' (Using password: NO)
> > A link to the server could not be established

>
> > It complains about the last line. And this message disappears if I do
> > not coll the function fff (I replace fff(); by //fff();).

>
> > Can anybody explain this behavior? It seems that connection made to
> > the database in the function somehow influences on other connection.

>
> I'm guessing here: You use literal strings here in your post making the
> mysql connection, but do you perhaps use variables in your real code? If
> so, (and assuming globals are off) the function doesn't see these
> variables unless you specify them global in the function.
>
> Other than that, I can't think of anything right now. I cannot reproduce
> the error anyway with my code (below).
>
> <?PHP
> require 'dbdata.inc.php';
> $link = mysql_connect($dbhost,$dbuser,$dbpasswd);
> function dbopen() {
> global $dbhost, $dbuser, $dbpasswd;
> $new_link = mysql_connect($dbhost,$dbuser,$dbpasswd,true);
> mysql_close($new_link);}
>
> dbopen();
> mysql_select_db($dbname,$link);
> $result= mysql_query('select count(*) from customers',$link);
> $row = mysql_fetch_row($result);
> echo $row['0']; // outputs 13, oughtta be closer to 50 if you ask me;-)
> ?>

Thank you for your code. I have compared it with my code (line by
line) and found out that the origin of the mistake was that I did not
put $link, as the second argument of the function mysql_query (in the
last line).

Thanks!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:12 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0