473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
$link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
$link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
function fff()
{
$link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tm p_user","tmpxxx " );
mysql_close( $link_new );
}
fff();
mysql_select_db ( "sss", $link );

Thanks.

Apr 26 '07 #1
7 13488

"Kurda Yon" <ku******@yahoo .comwrote in message
news:11******** *************@n 35g2000prd.goog legroups.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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link_new = mysql_connect( "localhost","tm p_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.
Apr 26 '07 #2
On Apr 26, 9:20 pm, "Steve" <no....@example .comwrote:
"Kurda Yon" <kurda...@yahoo .comwrote in message

news:11******** *************@n 35g2000prd.goog legroups.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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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.
Apr 27 '07 #3
Kurda Yon wrote:
On Apr 26, 9:20 pm, "Steve" <no....@example .comwrote:
>"Kurda Yon" <kurda...@yahoo .comwrote in message

news:11******* **************@ n35g2000prd.goo glegroups.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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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.
Apr 27 '07 #4
On Apr 27, 12:07 pm, Schraalhans Keukenmeester
<bitbuc...@inva lid.spamwrote:
Kurda Yon wrote:
On Apr 26, 9:20 pm, "Steve" <no....@example .comwrote:
"Kurda Yon" <kurda...@yahoo .comwrote in message
>news:11******* **************@ n35g2000prd.goo glegroups.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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
| mysql_select_db ( $db_name, $link);
| function fff()
| {
| $link_new = mysql_connect( "localhost","tm p_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","tm p_user","tmpxxx " );
mysql_select_db ( $db_name, $link);
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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.

Apr 27 '07 #5
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","tm p_user","pswxxx " );
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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@localho st' (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.

Apr 27 '07 #6
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","tm p_user","pswxxx " );
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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@localho st' (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($ne w_link);
}
dbopen();
mysql_select_db ($dbname,$link) ;
$result= mysql_query('se lect count(*) from customers',$lin k);
$row = mysql_fetch_row ($result);
echo $row['0']; // outputs 13, oughtta be closer to 50 if you ask me;-)
?>

Apr 27 '07 #7
On Apr 27, 3:02 pm, Schraalhans Keukenmeester <inva...@invali d.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","tm p_user","pswxxx " );
function fff()
{
$link_new = mysql_connect( "localhost","tm p_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@localho st' (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($ne w_link);}

dbopen();
mysql_select_db ($dbname,$link) ;
$result= mysql_query('se lect count(*) from customers',$lin k);
$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!

Apr 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17352
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? The user of my (hopefully to be opensourced) program has the ability to create database/table/column names on the fly. I'm aware of obvious characters such as ., , things like >, etc., which won't work, but haven't been able to source a...
3
3680
by: xo55ox | last post by:
Hi, I just build a website and need to somehow trap the user logon and 1) display 'welcome! so and so' at the left hand corner of the page. 2) valid whether the user exists in my MySQL database, user table. How do I do that? Thanks for your help!
19
8448
by: Rodger | last post by:
Hi all, I am getting the following error. ***************************** Microsoft JET Database Engine error '80004005' 'O:\DATA\DSR\DSR_DATA.MDB' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
13
28262
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql command methods. This is exactly what the Microsoft Data Access Application Block assembly does but I coded my own simple, custom class. I have a method named "ExecuteAggregate" that takes in a sql string like
3
16696
by: Alejandro Penate-Diaz | last post by:
Hi. I am using this function tu execute updates to a MySql database from a sql string, but I am getting a System.InvalidOperationException: Connection must be valid and open. Database queries work fine, but I cant update the source. Am I missing something? Thanks, Alejandro.
4
2184
by: Kentor | last post by:
I have some info that im grabbing from a mysql database with php and i want to make an xml file but i know that i need to be carefull with the & sign and so on... how can i clean the data coming from the db to have a valid xml file?
3
1437
by: ciki | last post by:
require_once "common.php"; $userid = $_REQUEST; $code = $_REQUEST; $sql = "UPDATE users SET activated='1' WHERE id='$userid' AND act_code='$code'"; db_query($sql); $sql_doublecheck = "SELECT * FROM users WHERE id='$userid' AND act_code='$code' AND activated='1'"; $result = db_query($sql_doublecheck);
6
3684
by: rhepsi | last post by:
Hi All... I Came across this error while populating a combobox from a datatable (I'm working in VB.NET): Specified argument was out of the range of valid values. Parameter name: '-1' is not a valid value for 'index'
11
4756
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 vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do. $testvend = SELECT language FROM vendor_details WHERE id = $vendorid ...
3
1815
by: anniemae | last post by:
What is this? one error message pops up: <configuration><system.windows.forms jitDebugging="true" /> another one is: "<?include[inc/db" to type 'Double' is not valid another is:
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.