简体   繁体   中英

Adding an If/else error/success message to PHP MYSQL function

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.

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(...)) {
    ...
}
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";
}

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";
}

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
}

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?"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM