Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

Creating Functions...

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Do you want it to connect to a database then? A better name for the function would be my_conn() maybe?
[code]
function mycon() {
  $user="xxx";
  $host = "localhost";
  $password="yyy";
  $database = "database_name";
  mysql_connect($host,$user,$password);
  mysql_select_db($database);
}
[/code]
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.
[code]
function myconn($user,$host,$pass,$db) {
  mysql_connect($host,$user,$pass);
  mysql_select_db($db);
}
[/code]
And you call it like...
[code]
myconn('thorpe','localhost','xxx','foo');
[/code]

Share this post


Link to post
Share on other sites
Or I use something like this:

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

And have a configuration file which is included:

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

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
So....

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

mycon('database_name');
?>
[/code]

Share this post


Link to post
Share on other sites
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) [snapback]384267[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Yes that is correct.
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.