Database connection from within a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Database connection from within a function

Post 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!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

people might be able to help you better when you post the code you have already. Maby somehting went wrong there.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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;
 }
}

?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

tim wrote:i dont like functions
8O ???
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Your probably having problems with variable scope within and outside the function.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Do you not just find yourself repeating a lot of code Tim?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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?)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

So i guess you dont use any classes/OO either?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<-- loves functions and classes. :)
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

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