AV1611 Posted June 15, 2006 Share Posted June 15, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/ Share on other sites More sharing options...
trq Posted June 15, 2006 Share Posted June 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45964 Share on other sites More sharing options...
poirot Posted June 15, 2006 Share Posted June 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45968 Share on other sites More sharing options...
trq Posted June 15, 2006 Share Posted June 15, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45970 Share on other sites More sharing options...
poirot Posted June 15, 2006 Share Posted June 15, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45980 Share on other sites More sharing options...
AV1611 Posted June 15, 2006 Author Share Posted June 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45981 Share on other sites More sharing options...
wildteen88 Posted June 15, 2006 Share Posted June 15, 2006 Yes that is correct. Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-45984 Share on other sites More sharing options...
AV1611 Posted June 15, 2006 Author Share Posted June 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-46001 Share on other sites More sharing options...
poirot Posted June 15, 2006 Share Posted June 15, 2006 Correct. Quote Link to comment https://forums.phpfreaks.com/topic/12078-creating-functions/#findComment-46004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.