stig1 Posted April 3, 2009 Share Posted April 3, 2009 I am trying to create functions that use two includes (database connection scripts). I require adding both include statements at the top of the file inside the function for the function to work. Surely there is a way to pass both the include statements into the functions I have created without adding extra lines of code each time I require the use of these files. Question I have is, how do I pass include into my functions so I only have to type includes once at the type of the page? Please find attach some of the code I have created. The code all works, apart from the functions not being able to find the include statements at the top of the page. I have changed some values to not give away the database structure I am using. Also the crdat8 and lupdat8 does not pass into the functions either. I also would not like to have to create these each time inside each function, as I require the dates to be the same on the whole database / table structure. <?php include("../includes/connect_mysql.php"); include("../includes/connect_systemi.php"); // Create date & time function $crdat8 = date("Y-m-d H:i:s"); $lupdat8 = date("Y-m-d H:i:s"); // Run update function update(); // Define the functions that run in the update function function update() { subtypsec_systemI(); } // Create & update function subtypsec_systemI() { $subtypsec_sql = "select * from tablename where division = 'R'"; $subtypsec_result = odbc_do($connect_systemi, $subtypsec_sql); while(odbc_fetch_row($subtypsec_result)) { $prodtype = odbc_result($subtypsec_result,1); $subtype = odbc_result($subtypsec_result,2); $division = odbc_result($subtypsec_result,3); $sequence = odbc_result($subtypsec_result,4); // Check $i_subtypsec_sql = "select * from tablename where prod = '".$prod."' and sub = '".$sub."' and div = 'R'"; $i_subtypsec_result = mysql_query($i_subtypsec_sql) or die (mysql_error()); $i_subtypsec_count = mysql_num_rows($i_subtypsec_result); // Update or insert if ($i_subtypsec_count == 1) { $update_subtypsec_sql = "update tablename set seq = '".$seq."', lupdat8 = '".$lupdat8."', hide = 'N' where prod = '".$prod."' and sub = '".$sub."' and div = 'R'"; $update_subtypsec = mysql_query($update_subtypsec_sql) or die (mysql_error()); } else { $insert_subtypsec_sql = "insert into tablename (div, prod, sub, seq, crdat8, lupdat8) values ('".$div."', '".$prod."', '".$sub."', '".$seq."', '".$crdat8."', '".$lupdat8."')"; $insert_subtypsec = mysql_query($insert_subtypsec_sql) or die (mysql_error()); } } } Help is always appreciated, and a huge thankyou in advance. Quote Link to comment https://forums.phpfreaks.com/topic/152333-create-function-with-includes/ Share on other sites More sharing options...
Humpty Posted April 3, 2009 Share Posted April 3, 2009 G'day Stig1, I'm a noob, and if a pro can provide a different answer the listen to them. It's my belief that you can't do it like that. Classes and Functions don't carry variables and stuff from elsewhere. e.g. Variable of $DatabaseName = "Frog"; doesn't carry through to classes or functions automatically and you have to create it again. As such you can have the same variable name $DatabaseName in your code and your function and each in turn can hold a different value I also believe that once the function is ended that value disapears and has to be recreated/reassigned next time you run that function. There are 'Global' or similar types of variables that can be used I believe (possibly only in classes). I have had an issue within classes as well (you can set a function in a class with the same name as that class and any time you call to that class that function runs first) ... I did set Database connections in there but they still didn't carry through to the function I was calling. I hope this helps, if need be ask another Q of what I said I know I talk lots and may have confused / babled. If nothing else perhaps someone else will jump in telling you how wrong I am and also providing you with a correct answer just for the sake of showing me up / correcting me Quote Link to comment https://forums.phpfreaks.com/topic/152333-create-function-with-includes/#findComment-800103 Share on other sites More sharing options...
stig1 Posted April 4, 2009 Author Share Posted April 4, 2009 Does anyone else have any ideas? Or would it be best for me to create a function that holds the database connection / date / time function.. and just run the function inside the other functions first? Quote Link to comment https://forums.phpfreaks.com/topic/152333-create-function-with-includes/#findComment-800833 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Why don't you create a MySQL class with all the methods you need. This would make things 100x easier and faster. There are plenty of examples on phpfreaks. I require adding both include statements at the top of the file inside the function for the function to work. Surely there is a way to pass both the include statements into the functions I have created without adding extra lines of code each time I require the use of these files. For this take a look at autoload(). Quote Link to comment https://forums.phpfreaks.com/topic/152333-create-function-with-includes/#findComment-800836 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.