allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Create a separate file called dbconnect.php Put this in your dbconnect.php file. <?php function db_connect() { Global $host, $username, $password, $dbname, $link_id, $table_name; $host = "localhost"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; $table_name = "xxxxx"; $db = mysql_connect($host,$username,$password); if (!$db) { echo "connect: mysql_error()"; exit; } $select_db = mysql_select_db($dbname,$db); if (!$select_db) { echo "select: mysql_error()"; exit; return ($link_id); } } ?> Now in your main program include the dbconnect.php file. include_once("dbconnect.php"); $link_id = db_connect(); Now run your function: function viewSpecialsUi() { $link_id = db_connect(); $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); } Now do me a favor and just fill in $table_name manually just to get the whole process working. Then we can work on passing that variable over to the function.... Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764902 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 The simplest way (without creating a class) of doing it would be to define your connection details within constants. Constants are global in scope. settings.php <?php define('DBNAME', 'foo'); define('DBHOST', 'foo'); define('DBUSER', 'foo'); define('DBPASS', 'foo'); define('DBTABLE', 'foo'); ?> functions.php <?php require_once 'settings.php'; function dbConnect() { mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); mysql_select_db(DBNAME) or die(mysql_error()); } function viewSpecialsUi() { dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM " . DBTABLE . " ORDER by date_added"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); } ?> Having said that. Unless you plan on using one table for your entire application I don't really see much point. Also, you realise your viewSpecialsUi() doesn't actually serve any purpose? I take it its just an example. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764906 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 The simplest way (without creating a class) of doing it would be to define your connection details within constants. Constants are global in scope. settings.php <?php define('DBNAME', 'foo'); define('DBHOST', 'foo'); define('DBUSER', 'foo'); define('DBPASS', 'foo'); define('DBTABLE', 'foo'); ?> functions.php <?php require_once 'settings.php'; function dbConnect() { mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); mysql_select_db(DBNAME) or die(mysql_error()); } function viewSpecialsUi() { dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM " . DBTABLE . " ORDER by date_added"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); } ?> Having said that. Unless you plan on using one table for your entire application I don't really see much point. Also, you realise your viewSpecialsUi() doesn't actually serve any purpose? I take it its just an example. Hi Thorpe, Well I really appreciate your help. But that's not working for me either. I did not post up all of my code.. there are query results etc below what I have that echo out. I tested my script with my details direct in the viewSpecialsUi() function and all works perfect. So it's just something that's not coming together for whatever reason Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764910 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 So it's just something that's not coming together for whatever reason Judging from your previous posts I would say that reason is scope related. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764911 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Once I figure out something that works, I will post it up. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764912 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 I agree. Scope issue and programming logic. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764914 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 Once I figure out something that works, I will post it up. The code I posted (using constants) should work. If yours doesn't youv'e done something wrong, post your code. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764915 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Once I figure out something that works, I will post it up. The code I posted (using constants) should work. If yours doesn't youv'e done something wrong, post your code. here is what i tried with these: define('DBNAME', 'xxxxxx'); define('DBHOST', 'xxxxxx'); define('DBUSER', 'xxxxxx'); define('DBPASS', 'xxxxxx'); define('DBTABLE', 'xxxxxx'); function dbConnect() { $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); $db = mysql_select_db(DBNAME) or die(mysql_error()); } function viewSpecialsUi() { dbConnect(); $bd_string = "<ul>"; $sql = "SELECT id, date, date_added, title, image FROM " . DBTABLE . " ORDER by date_added"; $result = mysql_query($sql, $connection) or die(mysql_error()); $num = mysql_num_rows($result); if ($num < 1) { echo "<p>There are no records to display.</p>"; } else ... Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764917 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 viewSpecialsUi() is incomplete. What errors do you get if any? Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764919 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 This works perfect, and my results are printed OK FYI: function viewSpecialsUi() { //dbConnect(); $db_name = "xxxxxxxx"; $connection = mysql_connect("localhost", "xxxxxxxx", "xxxxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); $table_name = "xxxxxxxx"; $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); if ($num < 1) { echo "<p>There are no records to display.</p>"; } else ..... <?php viewSpecialsUi(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764921 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Here is the php error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs\xxxxxxxx.com\xxxx\xxxx\phpFunctionsUi.php on line 61 Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764922 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 In the code where you are using constants (two posts up) you are still referencing $connection in this line.... $result = mysql_query($sql, $connection) or die(mysql_error()); For about the fifth time. This variable does not exist within this function!!!! Remove it. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764923 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 In the code where you are using constants (two posts up) you are still referencing $connection in this line.... $result = mysql_query($sql, $connection) or die(mysql_error()); For about the fifth time. This variable does not exist within this function!!!! Remove it. If it's going to make you crazy "!!!!!!" I think it's best if I figure this out on my own. And you defined $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); in you sample, so how does $connection not exist? Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764924 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 look im just going to figure this out... im getting more confused at this point. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764926 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 And you defined $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); in you sample, so how does $connection not exist? I think if you actually look you will see I did not define $connection within dbConnect(), and even if I did, it would only exist within dbConnect(). The simple thing is this. Variables created within functions only exist within that same function. No where else. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764927 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 I really see what you mean now on the limitations of the scope. I just don't see why this would not work: $table_name = "xxxxxx"; function dbConnect() { $db_name = "xxxxxxxxxx"; $connection = mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); global $table_name; } Shouldn't $table_name be accessible in my other function when dbConnect() is called? Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764928 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 No, its exists within dbConnect() only. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764929 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 Sorry, didn't see the global keyword. It will exist outside now, but its not set anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764930 Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 The last version of your code was close but missing a key detail: Function dbConnect() you make a variable $connection but you never pass this variable back using: return $connection; In the viewSpecialsUi function you need to call the function like so: $connection = dbConnect(); Try this. If it does not work, then in the dbConnect() function do the return like this: return &$connection; Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764935 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 The last version of your code was close but missing a key detail: Function dbConnect() you make a variable $connection but you never pass this variable back using: return $connection; In the viewSpecialsUi function you need to call the function like so: $connection = dbConnect(); Try this. If it does not work, then in the dbConnect() function do the return like this: return &$connection; This solution was posted several posts ago. I'm not sure the OP is reading the solutions provided. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764936 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 Thanks thorpe, The last version of your code was close but missing a key detail: Function dbConnect() you make a variable $connection but you never pass this variable back using: return $connection; In the viewSpecialsUi function you need to call the function like so: $connection = dbConnect(); Try this. If it does not work, then in the dbConnect() function do the return like this: return &$connection; This solution was posted several posts ago. I'm not sure the OP is reading the solutions provided. Thanks I will try that. I created this test script just to see if I could get my global to kick and NO luck: // please let me know if this is working for you! $table_name = "This is the output of a global!"; function dbConnect() { global $table_name; echo "$table_name"; } function globalEcho() { echo "$table_name"; } globalEcho(); // <-- this does not output! dbConnect(); // <-- this outputs! Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764938 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 If your going to start using globals don't bother using functions. Most of the encapsulation is out the window. The entire point of functions is to ecapsulate code and not to poison your global namespace with variables. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764940 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 I'm tracking with you. I just am trying to figure out why it's not working to eliminate possibilities. I tried both options you presented and neither of them worked. Same error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs.... If your going to start using globals don't bother using functions. Most of the encapsulation is out the window. The entire point of functions is to ecapsulate code and not to poison your global namespace with variables. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764942 Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 I'm with Thorpe. I was referring to the version you posted where you used the Constants for the connection details, and defined the two functions. Please go back to that, as it is the most sensible code you've posted, and very close to working. Globals aren't going to make things better at this point. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764943 Share on other sites More sharing options...
programguru Posted February 18, 2009 Author Share Posted February 18, 2009 I'm with Thorpe. I was referring to the version you posted where you used the Constants for the connection details, and defined the two functions. Please go back to that, as it is the most sensible code you've posted, and very close to working. Globals aren't going to make things better at this point. The problem is the constants function was not working either. I tried many variations with no luck. I hate to tie up the forum with my problem. But I'm just on a relentless battle now to figure out something that works, so I don't have to have a redundant $table_name in all of my functions when I could just call one dbConnect() function that holds it. Quote Link to comment https://forums.phpfreaks.com/topic/145686-solved-call-function-within-another-function/page/2/#findComment-764946 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.