Page 1 of 1
Database connection from within a function
Posted: Fri Jul 09, 2004 8:02 pm
by dardsemail
Hi,
I'm having some general problems setting a database connection from within a function. If I take the function away - it works fine, but I'm getting nothing when its wihtin a function.
Can someone give me some suggestions on how to create persistent database connections that I can call from within a function?
Thanks!
Posted: Fri Jul 09, 2004 8:33 pm
by ol4pr0
people might be able to help you better when you post the code you have already. Maby somehting went wrong there.
Posted: Fri Jul 09, 2004 8:37 pm
by tim
i dont like functions, but an untested version:
Code: Select all
<?php
function connect() {
$host = "localhost";
$user = ""; // u fill these in
$pass = "";
$name = "";
$link = mysql_connect($host, $user, $pass);
// some error checking
if (!$link) {
echo "could not connect";
exit;
}
if (!mysql_select_db($name)) {
echo "could not select db";
exit;
}
}
?>
Posted: Fri Jul 09, 2004 11:13 pm
by redmonkey
tim wrote:i dont like functions

???
Posted: Sat Jul 10, 2004 1:01 am
by kettle_drum
Your probably having problems with variable scope within and outside the function.
Posted: Sat Jul 10, 2004 11:46 am
by tim
no I dont red
I never really got into the swing of creating functions to do things
however, some things I did make some functions for due to they are easy to call on and are "universal"
but I normally dont make them if i dont feel I have to
call me silly
Posted: Sat Jul 10, 2004 12:01 pm
by kettle_drum
Do you not just find yourself repeating a lot of code Tim?
Posted: Sat Jul 10, 2004 12:08 pm
by tim
well Like i said, I do use n implement functions for something that would require a universal use.
but in the end, i dont repeat alot of code by avoiding the use of functions
I just dont prefer coding them and my code is not sloppy or not-professional in the least due to that.
Dont get me wrong, I will create a function if for like, theres alot of formatting for a text field, etc, or whatever the case is if it needs to be "universal"
but oh well, back to the topic at hand.
Posted: Sat Jul 10, 2004 12:10 pm
by tim
off topic again, sorry.
like creating a function to connect to different dbs, if i had 2 dbs I wouldnt use a function, i would want to see all the code at hand. Its easier for me to read
however, being 3+ dbs, a unversial function to connect would be ideal
/ on topic (maybe?)
Posted: Sat Jul 10, 2004 12:41 pm
by kettle_drum
So i guess you dont use any classes/OO either?
Posted: Sat Jul 10, 2004 12:46 pm
by Joe
Functions etc... are a pain in the ass but I feel I have to use them often as I seem to find myself creating continuous codes which are the same everywhere.
Posted: Sat Jul 10, 2004 12:53 pm
by kettle_drum
*wonders why everybody doesnt like functions*
Typical reasons why one would use them:
-Helps to break code up.
-Can re-use them over and over.
-They are black boxed - doesnt matter what happens inside them as long as they accept/return the same data.
-Helps programs to be co-developed so each team member can work on a set of functions.
-It means you have building blocks, rather than just raw materials. You can create a load of functions and then just glue them together.
-recursion.
Disadvantages...........<your post here>
Posted: Sat Jul 10, 2004 8:55 pm
by feyd
<-- loves functions and classes.

Posted: Sat Jul 10, 2004 11:02 pm
by dardsemail
Holy jumping Batman...
I guess my real concern is having the database information on each page - so I'd like to use an include file (as a .php) within the function to establish the connection. I've realized that I need to have the include line within the function - which is completely fine with me.
Thanks for all of your feedback! As for me, I'm all for functions. They seem to make my life a lot easier in terms of not having to recode over and over and over and over.... well, you get the picture
Thanks again!