Page 1 of 1

After connecting to MySQL...

Posted: Fri Apr 26, 2002 6:39 am
by Jim
Are you continuously connected?

When does your session end? If I were trying to create a forum like PHPBB, would I have to connect to the database on each page of the script?

Posted: Fri Apr 26, 2002 9:36 am
by jason
No, once you connect, you stay connected until the end of the entire script.

You could create a db_connect.php file, and include that file at the start of every script. After you run mysql_connect(), you pretty much stay connected.

Posted: Fri Apr 26, 2002 9:44 am
by twigletmac
jason wrote:No, once you connect, you stay connected until the end of the entire script.
Or until you use the mysql_close() function to end the connection.

Mac

Posted: Fri Apr 26, 2002 10:48 am
by Jim
So if I leave this forum and go to the http://www.devnetwork.net, I'm still connected to whichever database you guys use for this forum?

And if I create a file that connects a user to a database, his connection doesn't ever expire?

Or does it end at certain times (like mysql_close() or perhaps after closing a browser window)?

I'm sorry I'm thickheaded... just trying to get it all together. :P

Posted: Fri Apr 26, 2002 11:29 am
by samscripts
Are you continuously connected?
Unless you call mysql_close() or some kind of major error occurs, the connection will last from when you open it until the end of that script.

Once the script finishes the connection is gone, so you will need to connect to the database for every script that needs to get some data.

Your user will only be connected to the db while that script executes on that occasion. (ie. the db connection will not persist over different scripts)

Sam

Posted: Fri Apr 26, 2002 1:28 pm
by sam
Jim wrote:So if I leave this forum and go to the http://www.devnetwork.net, I'm still connected to whichever database you guys use for this forum?

And if I create a file that connects a user to a database, his connection doesn't ever expire?

Or does it end at certain times (like mysql_close() or perhaps after closing a browser window)?

I'm sorry I'm thickheaded... just trying to get it all together. :P
A connection runs as long as the php page is being parsed, as soon as the html is sent to the browser from the server the connection is lost.

Cheers moe

Ending the connection to the database...

Posted: Fri Apr 26, 2002 1:30 pm
by twigletmac
Jim wrote:So if I leave this forum and go to the http://www.devnetwork.net, I'm still connected to whichever database you guys use for this forum?
Once the script has finished running, i.e. once the page has been fully loaded, the database connection is ended so no. The script doesn't continuously run therefore the connection is not eternal.
Jim wrote:And if I create a file that connects a user to a database, his connection doesn't ever expire?

Or does it end at certain times (like mysql_close() or perhaps after closing a browser window)?
If you create a file that connects a user to a database the connection will last for as long as the code in the file (the script) is being run. If the user then goes to another page that requires a database connection a new one will be started and ended in the same way.

If you want to be sure that connections are being ended you should use the mysql_close() function once you've finished querying the database but it's not strictly necessary.

http://www.php.net/manual/en/function.mysql-connect.php
http://www.php.net/manual/en/function.mysql-close.php

Hope this helps :),

Mac

Posted: Fri Apr 26, 2002 4:24 pm
by Jim
Thanks amigo.

So, in the case that I have several pages that would require a connection to the database to work properly (as I assume these forums do) would I have to put the connection code on every page?

It doesn't seem that every .php page in the phpBB files have a MySQL connection code at the top...

Please explain how this works. I'm 'tarded :P

Thanks guys!

Posted: Fri Apr 26, 2002 4:35 pm
by twigletmac
Jim wrote: would I have to put the connection code on every page?

It doesn't seem that every .php page in the phpBB files have a MySQL connection code at the top...
Create a file with the connection code in it and include that at the top of all the pages that need a database connection. That way if you change the database name, or the username or password used to access it you only have to adjust one file.

Nice and easy :D

Mac

Posted: Fri Apr 26, 2002 6:15 pm
by Jim
I figured that would be the thing to do :)

Thanks amigo!

Posted: Fri Apr 26, 2002 6:21 pm
by mydimension
yeah it makes sense to put your connection code in one file and include it everywhere else but this is primarily so you don't have to type it over and over again. this is called keeping your code modular.