Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection to MySQL

Status
Not open for further replies.

bobo123

Programmer
Oct 4, 2002
30
Please, how can I detect, that I've been connected to MySQL before using mysql_connect() ?? Only through SQL question or $PHPSESSID or some function in PHP or some trick ??
Thanks Bobo
 
Hi Bobo123,
This is the only way I know to see if your php scripts connect to MySQL. It does connect using mysql_connect();
is there any reason why you don't want to use this?
Replace username and password with your MySQL username and password.

Hope this helps!

<html>
<head>
<title>
Connect To A MySQL Server
</title>
</head>
<body>

<?php

$linkID = mysql_connect(&quot;localhost&quot;,&quot;user&quot;,&quot;password&quot;);

if ($linkID != FALSE)
{
print &quot;Congratulations!!!<br>The connection to the server was made successfully.&quot;;
}
else
{
print &quot;The connection to the server failed.&quot;;
}

mysql_close($linkID);

?>

 
Hi overyde,
I haven't got problems with connection, but I wanna detect, if I'm connected BEFORE I make the connection to AVOID database traffic (i.e. don't connect if it's not necessary)
Bobo
 
Nice, but this is MySQL feature - to get it I need a connection.
 
There is no PHP function that does what you're looking for. For one thing, it is possible for an existing connection to be closed by MySQL for inactivity.

It's not a problem anyway -- the online manual page on mysql_connect() explicitly states: 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. (


Want the best answers? Ask the best questions: TANSTAAFL!
 
sleipnir214, DRJ478
Many thanks to both of you, I find both answers very valuable !!!
Bobo
 
there may be a method

if u r not connected then no query will execute,

if(mysql_query(some_sql))
//Connected
else
//Connect to databse

does this help?

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top