PEAR Packages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPEAR Packages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 30th, 2005, 05:49 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
DB Connection in function

I have read several tuturials on how to connect to a db using PEAR. All, however, assume I am using a single database. The standard procedure seems to be to create a config.php file with the connection string information:

$db_engine = 'mysql';
$db_user = 'username';
$db_pass = 'password';
$db_host = 'localhost';
$db_name = 'test';

and then call this file with:

require('config.php'); // db connection

at the beginning of the file where I have the code to manipulate that database. (I have tried this and it works fine - FOR 1 DATABASE)

---
The problem arises in that I want to connect to multiple databases and would prefer to have my connection string information in a separate function like:

function dbconnection($database)
{
-connection info-
}

and simply call it with:
dbconnection($database);

when I want to connect to a specific database.

When I tried this I received the following error:

Fatal error: Call to a member function on a non-object line XXX

where XXX is the line number of the calling object:
$db_object->query($sql);

Is there someone who can help me out? I hope I explained everything as clear as possible.

Thanks in advance,
Duane

Reply With Quote
  #2  
Old October 3rd, 2005, 12:05 AM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
RE: DB Connection in function

personally I would make 2 dns's in the configuration file and then instantiate 2 seperate objects for the 2 databases. As long as you make sure all your queries have the correct link reference - you should be fine.

config.php
php Code:
Original - php Code
  1.  
  2. $dsn1 = "mysql://username:password@localhost/test1";
  3. $dsn2 = "mysql://username:password@localhost/test2";
  4. // note the difference in DB name
  5.  

page.php
php Code:
Original - php Code
  1.  
  2. require_once('config.php');
  3.  
  4. $options = array(
  5.     'debug'       => 2,
  6.     'portability' => DB_PORTABILITY_ALL,
  7. );
  8.  
  9. $db1 = =& DB::connect($dsn1, $options);
  10. $db2 = =& DB::connect($dsn2, $options);

Reply With Quote
  #3  
Old October 4th, 2005, 03:25 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: DB Connection in function

I can see how this would work if I predefined all the databases I intend to use but, other than writing a function to open, edit and save the config.php file (to put the name of the database I intend to use in the file before I include it), is there any other way to dynamically tell "page.php" what database to initilize. I used the example of 2 databases but essentially I want to set up a template/function because I don't know how many databases I will need to access nor all their names. (probably numeric names)

Reply With Quote
  #4  
Old October 4th, 2005, 09:26 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
RE: DB Connection in function

Hmm - that is a harder one. Generally speaking for security reasons you don't want to keep the DB access info in the script... so couple questions.

1) do you want to store the DB access info so the user doesn't have to know it?
2) or do you want to interactively ask the user the specific DB info so they can control exactly which DB to connect to?

Basically it comes down to how much "help" do you want to give the user. I personal don't like letting people muck around with my data unless they are supposed to. In which case they will know the DB name, have a username and password. If they can't give me that - then they aren't supposed to be in there.

Anyway - I would ask for that info (or retreive the saved info from someplace like a DB table that has very tight access controls), dynamically generate the dns's and save them to the session variables in an array (so I can do a foreach or for loop), then run the script of of that data.

Am I making any sense?

Reply With Quote
  #5  
Old October 4th, 2005, 10:59 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: DB Connection in function

I would like to store the database information so the user does not need to know it. I will authenticate user w/ session and get access rights based on group of user. The table names are not really relevant and hard to remember for end users. (like difference between IP address and domains)

I can handle retrieving the login/password information from a secure file. (restricted permissions and outside of web directory) I like your idea about the dynamic dns strings. (I think we've come full circle) I'm not fully clear on why I need to save the strings to session variables and how to use them after that.

Can you give me an example of the dynamic dns string generation and how I can initialize it with or without require_once?

Maybe more details will help:

I am trying to create a survey anti-fraud module. When a user takes a survey, I want to ensure the person only takes the survey once every 30 days. I have an algorithm/function that returns 't' or 'f' if the record/survey should be flagged for manual review. I want to pass to the function the survey_id (numeric), record_id (numeric). I can then (within the function) query the appropriate database for the information I need. (duplicate name, phone number, e-mail, among others in last 30 days) I want the function to be modular in that all the fields will be standardized (so the strings in the antifraud function won't change) but I could check different surveys (numeric names) or across surveys.

Reply With Quote
  #6  
Old October 4th, 2005, 11:32 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
RE: DB Connection in function

OK - say you save the DB access info in a flat file outside the webroot in the format:

databaseName1|username|password
databaseName2|username|password
databaseName3|username|password

(I personally would only grant selects to these users so the user can only read the data - not manipulate it.)

then you can open the file, use file to get the lines into an array, then explode each line so you get the various parts. I say use the session variable because 1) they are kept on the server and 2) they will clear with the end of the session. Are they absolutly necessary - no but if you will be making connections for each page it will be quicker if you just look them up in session the go through reading the file all over again.

So foreach of the lines and it's data you can
move it into a session variable:
php Code:
Original - php Code
  1.  
  2. $_SESSION['DB']=array();
  3. array_push($_SESSION['DB'], array ('n'=>$dbName, 'u'=>$username, 'p'= $password));


Now that you have moved the DB data into sessions you can dynamically generate the dns on each page:
php Code:
Original - php Code
  1.  
  2. $num = count($_SESSION['DB'])
  3. for($i=0, $i<$num, $i++)
  4. {
  5.    ${dsn_.$_SESSION['DB']['n']} = "mysql://$_SESSION['DB']['u']:$_SESSION['DB']['p']@localhost/$_SESSION['DB']['n']";
  6. }


These of course are rough ideas - the code will have to be palyed with to make it work - but I think you get the idea.

Reply With Quote
  #7  
Old October 5th, 2005, 10:06 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: DB Connection in function

In the end I didn't use a session. Through trial and error I came up with the following solution:

I save the connection info - db in a file outside of web root. The when I am ready, I call the function passing through the database name and record id. In the function I require_once the partial connect script. (without database name) Then I append the database name on the script and voilĂ* I can use $db_object within the function to access the database. Following are parts of the function:

function antifraud($dbname,$recordid)
{
// Function used for transferring data from temp survey table to perm. data table

require_once('admin/dbaccess.php'); // partial database connect script.
$datasource = $datasource."/".$dbname;
$db_object = DB::connect($datasource, TRUE);
if(DB::isError($db_object)) {
die($db_object->getMessage());
}
$db_object->setFetchMode(DB_FETCHMODE_ASSOC);

$sql = "SELECT ..."
$query = $db_object->query($sql);

...

if score > # {
return 't';
}else{
return 'f';
}
}



Thanks so much, lig, for the brainstorming help. The main reason I was able to get around predefining the DNS strings is because I'm using the same username/password for transferring my survey data around. (very limited accounts) I still may use the session idea, however.

Reply With Quote
  #8  
Old October 5th, 2005, 10:24 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
RE: DB Connection in function

Congratulations! Glad to hear you figured it out. Let us know if you need anymore help.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPEAR Packages > DB Connection in function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek