M.O.S. Studios Posted April 1, 2009 Share Posted April 1, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/ Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798676 Share on other sites More sharing options...
trq Posted April 1, 2009 Share Posted April 1, 2009 function db_query($query,$msg,$auto='') This is all covered in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798678 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798679 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798681 Share on other sites More sharing options...
Daniel0 Posted April 1, 2009 Share Posted April 1, 2009 Or considering it's used as a boolean, default it to false. I wouldn't call that optional. you are still sending the third value, you are just sending it as a NULL. No you're not. Test it out. Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798682 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 It is optional. This would be a valid call: db_query("Some Query", "Some Message"); It would not throw an error. Thus the $auto is optional, as you do not have to pass it in for the function to work. Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798684 Share on other sites More sharing options...
lonewolf217 Posted April 1, 2009 Share Posted April 1, 2009 got it Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798686 Share on other sites More sharing options...
M.O.S. Studios Posted April 1, 2009 Author Share Posted April 1, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798701 Share on other sites More sharing options...
Daniel0 Posted April 1, 2009 Share Posted April 1, 2009 Really though, you should do $auto=false and then if (!$auto). It makes more sense seeing as it's a question of yes (true) or no (false). Quote Link to comment https://forums.phpfreaks.com/topic/152079-solved-optional-feild-in-a-funtion/#findComment-798707 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.