After connecting to MySQL...

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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

After connecting to MySQL...

Post 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?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post 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
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Ending the connection to the database...

Post 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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I figured that would be the thing to do :)

Thanks amigo!
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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.
Post Reply