Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-01-2011, 11:02 PM database connections
madnhain's Avatar
Experienced Talker

Posts: 42
Name: Jez
Location: Grand Junction, CO / Williston, ND
Trades: 0
Regarding connecting to a MySQL db...

my back end admin control panel for my website has a dynamic content setup with a navigation menu that allows the admin to access different information about clients and orders. each page that the admin selects has specific queries.

my question: how often do I have to connect to the database? do I need to make a connection on each page? or can I connect once (on the main admin page). and that connection still be active for each page? or do I have to connect on each page that is accessed?

I could do some trial and error coding, but with the number of pages involved, I thought it may be faster to ask here

Thanks in advance!
madnhain is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-02-2011, 06:03 AM Re: database connections
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Basically you need to connect to db every time you use queries, but yet again it depends on your page structure.

Consider adding db connection in a separate file and then just include that file on every page.
miki86 is offline
Reply With Quote
View Public Profile
 
Old 02-02-2011, 11:11 AM Re: database connections
Average Talker

Posts: 23
Trades: 0
miki86 is right. You need to connect to database on each page. Database connection is closed at the end of each page and you have to create it again on next page.
__________________

Please login or register to view this content. Registration is FREE
mwasif is offline
Reply With Quote
View Public Profile Visit mwasif's homepage!
 
Old 02-02-2011, 01:21 PM Re: database connections
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I think it simplifies things to think of it in terms of requests, 'page' is a little ambiguous.

For every request, you need call mysql_connect prior to executing any queries. If you're still having trouble understanding, try explicitly passing the connection resource to each mysql_* function call (doing this is normally optional).

PHP Code:
$conn mysql_connect('localhost''user''pass');
/* ... */
$res mysql_query($query$conn); 
This way you'll know you have to set $conn before running any queries. Just make sure $conn is global in scope otherwise you won't be able to access it in function or method calls.

Also, note that variables are persistent across includes. For example, the following should work fine:
PHP Code:
//main.php
$conn mysql_connect('localhost''user''pass');

require_once(
'foo.php'); 
PHP Code:
//foo.php

mysql_query($query$conn); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 02-02-2011 at 01:23 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to database connections
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23256 seconds with 12 queries