Jump to content

Objects in Functions


ltbaggz

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.