Jump to content


Photo

Creating Functions...


  • Please log in to reply
8 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 15 June 2006 - 04:01 PM

Please be gentle...

I have been reading on PHP.NET how to create a function and have looked at he example, but can't make sense of it:

Here is what I am trying to do:

create a login function for my scripts so you can do somthing like this:

my_login('database_name');


and it would do this for you:

$user="xxx";
$host = "localhost";
$password="yyy";
$database = "database_name";
mysql_connect($host,$user,$password);
mysql_select_db($database);

can someone help me get started?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 June 2006 - 04:07 PM

Do you want it to connect to a database then? A better name for the function would be my_conn() maybe?
function mycon() {
  $user="xxx";
  $host = "localhost";
  $password="yyy";
  $database = "database_name";
  mysql_connect($host,$user,$password);
  mysql_select_db($database);
}
Now... of course this function isn't very reusable because your details need to be changed within the function for each database you use. To make it more reusable you need to pass in some arguments.
function myconn($user,$host,$pass,$db) {
  mysql_connect($host,$user,$pass);
  mysql_select_db($db);
}
And you call it like...
myconn('thorpe','localhost','xxx','foo');


#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 15 June 2006 - 04:12 PM

Or I use something like this:

class db
{
   // this is a php4 constructor
   function db()
   {
      mysql_connect(DB_HOST, DB_USER, DB_PASS);
      mysql_select_db(DB_NAME);
   }
}

And have a configuration file which is included:

define('DB_HOST', 'localhost');
define('DB_USER', 'user');
// ...

~ D Kuang

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 June 2006 - 04:14 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Or I use something like this:[/quote]
Why on earth would you post an example of class usage when the guy is clearly having trouble with functions? :) Besides, thats a hell of allot of overhead for a simple database connection.

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 15 June 2006 - 04:33 PM

Yes, but I have all my db functions under that class.
Then, if I need to change something, or even change from MySQL to postGRE for example, it's very easy ;D
~ D Kuang

#6 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 15 June 2006 - 04:36 PM

So....

Would this be correct?
<?
function mycon($database){
    $user="xxx";
    $host = "localhost";
    $password="yyy";
    mysql_connect($host,$user,$password);
    mysql_select_db($database);
    }

mycon('database_name');
?>


#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 June 2006 - 04:37 PM

Yes that is correct.

#8 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 15 June 2006 - 05:17 PM

So...

I would take the function portion and save it as a file that I include() in the script, then just use it like any other function and I'm done... correct?

[!--quoteo(post=384267:date=Jun 15 2006, 12:37 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 15 2006, 12:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes that is correct.
[/quote]


#9 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 15 June 2006 - 05:20 PM

Correct.
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users