programguru Posted February 18, 2009 Share Posted February 18, 2009 hello, i cannot get past this. i have attempted many variations of including this function within my other function, and i cannot get it to work. // database connection function dbConnect() { $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxxxx"; } The function i am trying to use to include the above function: function viewSpecialsUi() { dbConnect(); // <<-- here is the function I am trying to include $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added"; $result = mysql_query($sql, $connection) or die(mysql_error()); $num = mysql_num_rows($result); ... I've aleady confirmedmy script is working by including the connection details within the viewSpecialsUi() function, but it just won't work as I would like it to. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/ Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 hmm.... function viewSpecialsUi() { $connection = dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added"; $result = mysql_query($sql, $connection) or die(mysql_error()); $num = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764860 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 I think you will have to create a class for that, I do not really think that is possible. http://us3.php.net/class Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764861 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 Sorry I just ran this and worked: <?php function test() { echo 'tets'; } function test2() { test(); } test2(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764862 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 hmmm.. i tested your script on my php settings and it worked OK... I cannot get away from this: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs\xxxxxxxxxx.com\xxxxxx\xxxxx\phpFunctionsUi.php on line 44 Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764867 Share on other sites More sharing options...
jackpf Posted February 18, 2009 Share Posted February 18, 2009 I think I know why this is- you need to define all variables for a function when calling it. eg- function foo() { connect('localhost', 'user', 'pass'); #...rest of function... } Functions don't reference global variables. Well, I don't think they do. Hope this helps. Jack. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764869 Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 hmmm.. i tested your script on my php settings and it worked OK... I cannot get away from this: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs\xxxxxxxxxx.com\xxxxxx\xxxxx\phpFunctionsUi.php on line 44 Right ok, make a change to your DB function, use this: // database connection function dbConnect() { $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxxxx"; return ($connection); <---add this } Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764871 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 I also created a class, and it did not work either... 1) So your script worked A-OK! Which means functions CAN be called within functions.. 2) I am able to run my script perfect placing the connection details directly in the viewSpecialsUi() function. But there seems to be some issue with calling these connection details in a function (maybe a security issues?????) Still stuck! SOS Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764872 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Right ok, make a change to your DB function, use this: // database connection function dbConnect() { $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxxxx"; return ($connection); <---add this } Hmmm.. nope did not work on that.. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764875 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 ...mm I was really thinking that was the solution! i'll try on my server. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764876 Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Ok well try this now... // database connection function dbConnect() { Global $connection; <---add this make it global $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxxxx"; return ($connection); <---add this } Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764878 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 Don't use globals within functions. Remove any reference to $connection from within viewSpecials() as that variable does not exist. function viewSpecialsUi() { dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); } or, have dbConnect() return $connection and use it like.... function viewSpecialsUi() { $conn = dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added"; $result = mysql_query($sql, $conn) or die(mysql_error()); $num = mysql_num_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764879 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 // database connection function dbConnect() { Global $connection; <---add this make it global $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxxxx"; return ($connection); <---add this } did this work for you? did not work on my end... I just tested a simple echo function within my script and the output was OK.. it's got to be related to the database connection details specifically and some restriction within php... but there has to be a way.. still seeking. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764881 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 OK I think now I really found it, you are trying to use $table_name variable in you second function, but it is not even declare in this function. You will have to create an array containing $connection and $table_name in your first function and then: retrun $myarray See if it works. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764882 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 You really need to read up on variable scope and how it relates to functions. Variables created within functions are not available outside of that function. That is almost the entire point of functions, to encapsulate code. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764885 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 You really need to read up on variable scope and how it relates to functions. Variables created within functions are not available outside of that function. That is almost the entire point of functions, to encapsulate code. I do understand encapsulation and why it exists, but the code you posted does not work. That is what I was using prior on my first attempts: function viewSpecialsUi() { dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764886 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 the reason it doesn't work is $table_name doesn't exist within that function. I hadn't seen it when I posted my other reply. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764887 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 OK I think now I really found it, you are trying to use $table_name variable in you second function, but it is not even declare in this function. You will have to create an array containing $connection and $table_name in your first function and then: retrun $myarray See if it works. I did define $table_name = "special"; in the dbConnect() function??? I am totally missing the point? Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764888 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 As I said, variables declared within a function are only available within that function. Meening that $table_name only exists within dbConnect(). I am totally missing the point? Indeed. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764889 Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Dude, stop messing around with variables, let's just get your query to work. Statically fill in what table_name you want!! $sql = "SELECT id, date, date_added, title, image FROM [b]MYTABLENAME[/b] ORDER by date_added"; You can make it a variable later after it works... Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764891 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 As I said, variables declared within a function are only available within that function. Meening that $table_name only exists within dbConnect(). I am totally missing the point? Indeed. thorpe, I appreciate your assistance. And I am glad there are forums like this. I'm just not getting how to globally use the var if I cannot use global to give it proper scope. Let me know if you come across the answer. I'm going to keep searching. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764893 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Dude, stop messing around with variables, let's just get your query to work. Statically fill in what table_name you want!! $sql = "SELECT id, date, date_added, title, image FROM [b]MYTABLENAME[/b] ORDER by date_added"; You can make it a variable later after it works... allworknoplay, the whole point is i want to keep all of the connection details contained in one location for "class" like usage. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764894 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 Indeed LMAO! Do not feel bad though, you are learning. What will really help you a lot are tutorials, just search in google: PHP function tutorials. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764896 Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 allworknoplay, the whole point is i want to keep all of the connection details contained in one location for "class" like usage. Fine, you really want to encapsulate? Give me 1 minute and I will tell you exactly what you have to do. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764897 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Indeed LMAO! Do not feel bad though, you are learning. What will really help you a lot are tutorials, just search in google: PHP function tutorials. Cheers! angelcool, Sure thanks for your help.. BTW I created an array and that did not work either.. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/#findComment-764900 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.