1

Im wondering how i could add a success or fail message to return at the end of this function

function InsertIntoDB($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9){
$sql = "INSERT INTO `caches` ( `url` ,  `username` ,  `password` ,  `cachename` ,  `lat` ,  `long` ,  `message` ,  `notes` ,  `tags`  ) VALUES(  '{$field1}' ,  '{$field2}' ,  '{$field3}' ,  '{$field4}' ,  '{$field5}' ,  '{$field6}' ,  '{$field7}' ,  '{$field8}' ,  '{$field9}'  ) "; 
mysql_query($sql) or die(mysql_error());
}

Id like it to return "OK" on success, and "FAIL" on failure.

1
  • 2
    Why not return true/false as success/failure? Returning strings means you are going to have == 'OK' littered throughout your code. Commented Sep 7, 2009 at 0:23

5 Answers 5

3

You shouldn't hard-code messages like "OK" and "FAIL" into a function as its possible return values, that's a bit scary in terms of localization and the flexibility of your application. You should return a boolean and deal with messages outside of that function.

It makes little sense to test the returned strings if you're calling that function from elsewhere in your application where you only care about the semantics of what has happened as opposed to the returned message. What if you decide to change 'FAIL!' to 'Unsuccessful' for example? You will end up having to change all the other code that relies upon the return of your function. Why not just do something like this:

function InsertIntoDB($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9){
     $sql = "INSERT INTO `caches` ( `url` ,  `username` ,  `password` ,  `cachename` ,  `lat` ,  `long` ,  `message` ,  `notes` ,  `tags`  ) VALUES(  '{$field1}' ,  '{$field2}' ,  '{$field3}' ,  '{$field4}' ,  '{$field5}' ,  '{$field6}' ,  '{$field7}' ,  '{$field8}' ,  '{$field9}'  ) "; 
     return mysql_query($sql);
}

if(InsertIntoDB(...)) {
    echo 'Success!';
} else {
    echo 'Fail!';
}

Then somewhere else in your application, you won't need to:

if(InsertIntoDB(...) == 'SUCCESS!') {
    ...
}

but instead:

if(InsertIntoDB(...)) {
    ...
}

or:

if(!InsertIntoDB(...)) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

What do you define to be success?

If you define mysql_query() returning true to be a success, then the following will work:

function InsertIntoDB($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9){
     $sql = "INSERT INTO `caches` ( `url` ,  `username` ,  `password` ,  `cachename` ,  `lat` ,  `long` ,  `message` ,  `notes` ,  `tags`  ) VALUES(  '{$field1}' ,  '{$field2}' ,  '{$field3}' ,  '{$field4}' ,  '{$field5}' ,  '{$field6}' ,  '{$field7}' ,  '{$field8}' ,  '{$field9}'  ) "; 
     return mysql_query($sql) ? "OK" : "FAIL";
}

1 Comment

read the other answers - though this is exactly what the question was asking for, returning a string isn't a great practice.
0
function InsertIntoDB($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9){
$sql = "INSERT INTO `caches` ( `url` ,  `username` ,  `password` ,  `cachename` ,  `lat` ,  `long` ,  `message` ,  `notes` ,  `tags`  ) VALUES(  '{$field1}' ,  '{$field2}' ,  '{$field3}' ,  '{$field4}' ,  '{$field5}' ,  '{$field6}' ,  '{$field7}' ,  '{$field8}' ,  '{$field9}'  ) "; 
$result = mysql_query($sql);
if($result) return "OK";
else return "FAIL";
}

Comments

0

Why not use boolean instead of "OK" and "FAIL"?

function InsertIntoDB($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9){
     $sql = "INSERT INTO `caches` ( `url` ,  `username` ,  `password` ,  `cachename` ,  `lat` ,  `long` ,  `message` ,  `notes` ,  `tags`  ) VALUES(  '{$field1}' ,  '{$field2}' ,  '{$field3}' ,  '{$field4}' ,  '{$field5}' ,  '{$field6}' ,  '{$field7}' ,  '{$field8}' ,  '{$field9}'  ) "; 
     return mysql_query($sql);
}

if(InsertIntoDB(....)){
   // succeeded
}else{
   // failed
}

Comments

0

As everyone else has suggested, to indicate success or failure, you really should use a boolean. If you really want that extra bit of semantic sugar for readability, then you could define some constants:

define('OK', true);
define('FAIL', false);

function insertIntoDB() {
    $sql = "...";
    return mysql_query($sql) ? OK : FAIL;
}

if (insertIntoDB() == OK) {
    // win!
}

But I'll just reiterate, that's only if you particularly feel you need this extra readability. This method would probably make future maintainers of your code say "WhyTF did he do that?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.