Page 1 of 1
Check to see if connect already open
Posted: Thu May 16, 2002 3:49 am
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
Posted: Thu May 16, 2002 5:00 am
by prasadharischandra
i think u have to use like that
$test=mysql_connect();
if ($test){
runn}
else{bfkdbk}
uhh sorta..
Posted: Thu May 16, 2002 6:55 am
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.
Posted: Thu May 16, 2002 7:43 am
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
Posted: Thu May 16, 2002 8:02 am
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?
Posted: Thu May 16, 2002 8:04 am
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....)
thx enygma
Posted: Thu May 16, 2002 8:30 am
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

Posted: Thu May 16, 2002 8:40 am
by enygma
yeah, just check to be sure that the path they give is in the $DOCUMENT_ROOT
Posted: Thu May 16, 2002 10:39 am
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.
Re: thx enygma
Posted: Thu May 16, 2002 10:40 am
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

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