db vars inside 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
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

db vars inside a function

Post by blacksnday »

The below is my first function I have written.
My problem (and question) is how the DB connection works.
When using the vars as shown, the function always returns
with No Database Selected
However if filling in the values in place of the vars in
mysql_connect and mysql_select_db
then the connection always returns as true.

With functions is there a certain way needed to make
vars for things like a db conn to work correctly?
Because I really dont want to have the true values for
db info in the function if not needed.

Code: Select all

$dbhost = "localhost";
$dbuser = "user";
$dbpass = "password";
$dbname = "dbname";

 function site_banner()
{
	mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to database server: " . mysql_error());
	mysql_select_db($dbname) or die("Couldn't connect to database: " . mysql_error());
      $result = mysql_query("SELECT * FROM Banner ORDER BY RAND() LIMIT 1");
      while ($row = mysql_fetch_assoc($result)){ 
echo $row['image']; 
}

}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You should pass the variables as parameters to your function. Another (but bad) solution would be to declare the variables as global in your function.

More reading (and examples) at http://be.php.net/manual/en/language.va ... .scope.php
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I see it now, and I changed it to

Code: Select all

function site_banner($dbhost, $dbuser, $dbpass, $dbname)
and also changed

Code: Select all

site_banner($dbhost, $dbuser, $dbpass, $dbname);
Which works great now.

Would this be the most recommended way?
Thanks for the help :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ya definatly the most recommended way

but another way would be to

Code: Select all

function site_banner()
{
  global $dbhost;
  global $dbuser;
  global $dbpass;
  global $dbname;

  //mysql stuff here
}
but that way is not recomended
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Yes I can see how that wouldn't be advisable.

One more quick question if you all have the time...

Since I am a phpNewbie and want to learn about
functions/variables/etc BEFORE I dive into OOP,
would making at least 2-5 functions that act as the same
as above, yet for different aspects such as
random banner, random news, random qoutes, etc..
be a good beginners way?

Or would that kind of multiple function slow down site
because of the many seperate and open DB queries.

If I understand correctly, OOP would be able to combine
the functions then assign based on what is needed and
provide the proper values needed for each to work.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if you want to get good with functions and variables before using classes (good idea) just write a couple simple scripts. like make a db driven news script and make a function to add news, a function to add comments, update blah blah. its good practice
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

haha! i must be on the right learning track then,
as thats how I told myself I would learn best.

the random image/random news function was built for my
starter news publishing script at http://s4m.us/news :P

next learning project is to create an admin/login script
to edit the banner function in mysql using web-based, which then i plan
to use that as the basis for rest of the admin side web scripts.

thanks for the help and i will probably poke my head around
here a few more times :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, all manipulations can be categorized as one of the following:

- Create
- Read
- Update
- Delete

So if you have a class that handles these, you can use for your news, quotes, banners, ...
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

timvw wrote:You should pass the variables as parameters to your function.
Just wanted to add a little thank you!

If you had told me exactly how to do it, I wouldn't have learned
and retained that knowledge for future reference.
timvw wrote:Well, all manipulations can be categorized as one of the following:

- Create
- Read
- Update
- Delete

So if you have a class that handles these, you can use for your news, quotes, banners, ...
Same goes for that advice.
As I have much experience with php I already know how classes
are seperate. However since i have no coding exp..(well, a tad bit more
exp as every day passes :) )
I realise classes should be something i stay away from for awhile.
With that said... I now know from your reply a reference to later use
to search for the answer which will then allow me to teach myself how
it needs to be done.

To many people want to learn in such a way that another does it for them,
but as for me... i want to learn in such a way that I actually learn by doing for myself, and I thank these forums for allowing such an outlet
for this to happen :D
Post Reply