Check to see if connect already open

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

Check to see if connect already open

Post by pHaZed »

Hey,
can anyone tell me how i can do a check to see if there is already a active connection to the mysql database?
Thx
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

Post by prasadharischandra »

i think u have to use like that
$test=mysql_connect();
if ($test){
runn}
else{bfkdbk}
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

uhh sorta..

Post by pHaZed »

Well that didnt reall work.
the mysql connect var is in a function in a file i include.
I have lots of mysql stuff through my site.
But dont want to use pconnect.
If anyway can advise me on a way of checking to see if connection is open would be great.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You could set a global variable to the result from the connect function, but this would be error prone. As far as I am aware a connection usually terminates as soon as the script has finished running, so using the same connection in another script could potentially fail.

The pconnect is probably your safest option if you really want to do this, why do you need to do this?

Mike
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

Post by pHaZed »

Thx mikeq
it seems i my best answer will be persistent connections.

My site works like so
i have index.php
and it has my design layout
basically consists of 3 columns in a html table.
the left navigation (left col) is included and same with right,
For mine middle section (main content)
i use this

Code: Select all

if ($id == true) 
{
    include ("$id.php");
} 
else
{
    include ("main.php");
}
so all links are like so index.php?id=filename.
So in conclusion
All other the place i have mysql queries pulling data and displaying it in html from the database.
If i just add a line to index.php to connect and use pconnect it should be fine for the whole site?
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

just test the resource variable name, like the first post suggested.

If you named them all the same, you shouldn't have a problem.
You can always just make the connection in the included file and, then, when it's included, you'll always have that connection.

If you open a connection in a PHP script, that only stays open as long as that script runs. Once it finishes, the connection is closed...

(Also, be careful with the include($file); line. Wouldn't want someone doing an index.php?id=/etc/passwd or something....)
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

thx enygma

Post by pHaZed »

Yes,
I have sorted all the connection related problems.
But it seems i have just realised a new problem.
(Also, be careful with the include($file); line. Wouldn't want someone doing an index.php?id=/etc/passwd or something....)
Could there be a way of limiting the files it includes to say.
Just $id=filename
No ../bla/bla ...etc?
Any ideas 8O
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

yeah, just check to be sure that the path they give is in the $DOCUMENT_ROOT
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

enygma wrote:just test the resource variable name, like the first post suggested.

If you named them all the same, you shouldn't have a problem.
You can always just make the connection in the included file and, then, when it's included, you'll always have that connection.

If you open a connection in a PHP script, that only stays open as long as that script runs. Once it finishes, the connection is closed...

(Also, be careful with the include($file); line. Wouldn't want someone doing an index.php?id=/etc/passwd or something....)
I think pHaZed was trying to open a connection on say his first script and still use the same resource id say 4 scripts later, which probably wouldn't work.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Re: thx enygma

Post by mikeq »

pHaZed wrote:Could there be a way of limiting the files it includes to say.
Just $id=filename
No ../bla/bla ...etc?
Any ideas 8O
Did you say you keep your list of valid pages in your MySQL table, if so just do a check against your table to make sure it is a valid page.

Mike
Post Reply