Jump to content

[SOLVED] optional feild in a funtion


M.O.S. Studios

Recommended Posts

hey guys,

 

I programmed this function, in it i want the 3rd value to be optional

 

function db_query($query,$msg,$auto)
{
	if(!mysql_query($query))
	{
  $error .=mysql_error();
}
else
{
 $error .=$msg;
}

if($auto){$GLOBALS[$auto]=mysql_insert_id();}

return $error;
}

as you can see, the $auto feild is not nessasary,

 

here is my problem, if i dont enter a value for $auto i get this error

 

Warning: Missing argument 3 for db_query(), called in /home/montrea1/public_html/demo/admin/content/products/add.php on line 65 and defined in /home/montrea1/public_html/demo/admin/common/funtions/db.php on line 3

 

any one know how to tell the function that that is an optional feild?

Link to comment
https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/
Share on other sites

there are no such things as optional fields.  if you dont want to use the field, then send some arbitrary value that the function will know is invalid

 

You sure about that? Check out the Functions in the manual.

 

function db_query($query,$msg,$auto=null)

 

That way you can just pass in the first 2. Then you can use is_null or isset if you want to know if $auto was passed in or not.

I wouldn't call that optional. you are still sending the third value, you are just sending it as a NULL.  The way I understood the OP's question is how would he call that function with 2 values instead of 3, which also seems to be why he got that error message

Awesome this works,

 

here is my final code

 

function db_query($query,$msg,$auto=NULL)
{
	if(!mysql_query($query))
	{
  $error .=mysql_error();
}
else
{
 $error .=$msg;
}

if(!is_null($auto)){$GLOBALS[$auto]=mysql_insert_id();}

return $error;
}

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.