ltbaggz Posted December 6, 2006 Share Posted December 6, 2006 I am trying to include a file that just defines some parameters for database access. my setup is as follows[code]<?php include_once("include/mysql_class.php"); include_once("include/db_info.php");function go($year, $month){ $my_db = new mysql($db, $db_host, $db_pass, $db_user); $name = $my_db->get_name(); echo $name; $my_db->connect();//there is more to the function but this is where the problem is}?>[/code]the variables, $db, $db_host, $db_pass, $db_user are defined in this file but it will not recognize them and prints nothing for $name, i use this same file in another places and the variables are accessed fine. the only difference is that this is a function but i cant see why that would matter. any ideas?Drew Link to comment https://forums.phpfreaks.com/topic/29729-problem-with-include-file-for-function/ Share on other sites More sharing options...
trq Posted December 6, 2006 Share Posted December 6, 2006 You would need to pass these variables into the function. eg;[code=php:0]function go($year, $month, $db, $db_host, $db_pass, $db_user)[/code] Link to comment https://forums.phpfreaks.com/topic/29729-problem-with-include-file-for-function/#findComment-136487 Share on other sites More sharing options...
drifter Posted December 6, 2006 Share Posted December 6, 2006 that is because it is in a function - this is scopetry as your first line of your function global $name;That will fix it - or you can pass $name into the function when you call it. Link to comment https://forums.phpfreaks.com/topic/29729-problem-with-include-file-for-function/#findComment-136488 Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 It's a scope problem.Variables defined outside of a function are not visible inside of the function unless they are either declared as globals inside the function or are passed as parameters.To set them as globals, do something like:[code]<?phpfunction go($year, $month){ global $dbm $db_host, $db_pass, $db_user; $my_db = new mysql($db, $db_host, $db_pass, $db_user); $name = $my_db->get_name(); echo $name; $my_db->connect();//there is more to the function but this is where the problem is}?>[/code]To pass them as parameters:[code]<?phpfunction go($year, $month, $db, $db_host, $db_pass, $db_user){ $my_db = new mysql($db, $db_host, $db_pass, $db_user); $name = $my_db->get_name(); echo $name; $my_db->connect();//there is more to the function but this is where the problem is}?>[/code]If you pass them by parameters, you will have to change any lines that call the function.Ken Link to comment https://forums.phpfreaks.com/topic/29729-problem-with-include-file-for-function/#findComment-136491 Share on other sites More sharing options...
ltbaggz Posted December 6, 2006 Author Share Posted December 6, 2006 alrighty sounds good, just decided to pass them in the function but i wanted to make sure i understood why. Drew Link to comment https://forums.phpfreaks.com/topic/29729-problem-with-include-file-for-function/#findComment-136492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.