ltbaggz Posted December 6, 2006 Share Posted December 6, 2006 I am having a problem creating a object inside a function. I do not have the code in front of me right now but this is the idea(forgive any errors the syntax is correct in my real code)[code]include_once("mysql_class.php")function maketable($y,$m){$db = new mysql($dbname, $dbhost, $dbuser, $dbpassword);$db->connect();$db->select_db();$query = "write the query";$db->query($query); //uses params passed into function//now draw the table and incorporate query data.}[/code]Now it throws an error while trying to select the database. if i move everything outside the function it works fine but then i dont have access to the params passed into the function. is there some reason why this doesnt work? It seems like the object is instantiated fine, i could write a little get function to see if the database name, user is being set. but in the mean time if anyone has an idea it would be appreciated.Drew Link to comment https://forums.phpfreaks.com/topic/29695-objects-in-functions/ Share on other sites More sharing options...
willfitch Posted December 7, 2006 Share Posted December 7, 2006 The values for $dbname, $dbhost, $dbuser, and $dbpassword aren't defined. Remember, you are using a function, so it's expecting those variables to either be provided through the parameter, or local scope.If these variables exist outside of the function, use the keyword "global" and import them into your local scope. Link to comment https://forums.phpfreaks.com/topic/29695-objects-in-functions/#findComment-136614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.