|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||||
|
|||||
|
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:
page.php |
|
#3
|
|||
|
|||
|
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)
|
|
#4
|
||||
|
||||
|
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? |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||||
|
|||||
|
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: Now that you have moved the DB data into sessions you can dynamically generate the dns on each page: php Code:
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
RE: DB Connection in function
Congratulations! Glad to hear you figured it out. Let us know if you need anymore help.
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PEAR Packages > DB Connection in function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|