0

I wonder if there's any draw backs of creating few functions for simple MySQL operations like SELECT, INSERT, DELETE, UPDATE e.g.

function sql_select($table, $values, $condition='WHERE true', $limit='') {
   global $sql_obj;

   $select_query = "SELECT {$values} FROM {$table} WHERE {$condition} {$limit}";
   $result = $sql_obj->run_query($select_query,"select");

   return $result;
}

sql_select('user_table', 'name, address, phone', 'user_id = ' .mysql_real_escape_string($_POST["user_id"]), 'LIMIT 0, 1' );

above function can be upgraded a bit to allow more functionality. I know benefits of it, but any drawbacks?

2
  • I can't see why helper functions would be an issue, but you should make use of prepared statements. Commented Dec 20, 2012 at 2:20
  • @Vulcan: This particular helper function would make prepared statements a pain to implement. :P There's not much use in them if you're gonna build SQL this way; they don't offer any protection once you've already thrown data in with your SQL. Commented Dec 20, 2012 at 2:27

3 Answers 3

3

There are no drawbacks worth speaking of. In fact, you should factor out commonly used code into functions as a general practice. As you pointed out, this will allow you to add additional enhancements without having to go through all the places in your code that use those functions.

The only thing to worry about (and this is a minor worry compared to code duplication) is if your functions are general enough or are coupled with other assumptions. For instance, your function relies on the global $sql_obj. The user doesn't know this; what if the user overwrites this global, or has another object s/he would rather your function use?

Sign up to request clarification or add additional context in comments.

2 Comments

Can you write few more lines on second part of your replay i am not sure what you mean
@PetjaZaichikov: I went ahead and added some more detail.
0

The only drawback I can think of is that it is difficult/annoying to run complex queries.

1 Comment

Exactly. But you can write another function to handle that kind of queries too.
0

Maybe you are searching for an Object Relational Mapper? Maybe the doctrine framework is interesting for you.

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.