Page 1 of 1

db vars inside a function

Posted: Sat Jul 30, 2005 6:23 am
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']; 
}

}

Posted: Sat Jul 30, 2005 7:07 am
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

Posted: Sat Jul 30, 2005 7:56 am
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 :)

Posted: Sat Jul 30, 2005 9:11 am
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

Posted: Sat Jul 30, 2005 9:47 am
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.

Posted: Sat Jul 30, 2005 11:28 am
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

Posted: Sat Jul 30, 2005 11:32 am
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 :)

Posted: Sat Jul 30, 2005 11:46 am
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, ...

Posted: Sun Jul 31, 2005 12:49 am
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