Jump to content

Creating Functions...


AV1611

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?
Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.