skip to Main Content

I am using oscommerce so you might need an understanding of it to answer this but I don’t think so it is pretty general.

Why would the original programmers create functions to do basic sql queries like this:

  function tep_db_query($query, $link = 'db_link') {
    global $$link, $logger;

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
      if (!is_object($logger)) $logger = new logger;
      $logger->write($query, 'QUERY');
    }

    $result = mysql_query($query, $$link) or tep_db_error($query, mysql_errno(), mysql_error());

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
      if (mysql_error()) $logger->write(mysql_error(), 'ERROR');
    }

    return $result;
  }

When I seem to be able to accomplish a query using the regular mysql_query?

My guess is so that you don’t have to keep redefining the db connection? I’m not sure though because I can just use mysql_query like I said. Obviously I’m pretty new at this so I’m missing something just curious what.

4

Answers


  1. If you go line by line through the code and look what it does, it is pretty clear that the function was written to log and execute queries. mysql_query would execute the sql, but does not do any logging.

    Login or Signup to reply.
  2. Because you don’t have to write mysql_query everytime, because you don’t have to write error handling everytime when you are going to write a mysql query. All the repeated things can be handled in that custom function. This is why we write custom function. You don’t have to write mysql_error() with every mysql_query call because you have your custom one which can handle all these things.

    We can handle generalize things in one custom function so we don’t have to write them again and again with each call of mysql_query.

    Login or Signup to reply.
  3. Within this function they not only call mySQL but record the query into a logging object, run checks against framework constants and define custom error handlers. This is a function for the reason that they did not want to write this code out every time they query the DB.

    Login or Signup to reply.
  4. In this case, to

    • log all DB queries when STORE_DB_TRANSACTIONS is set to true, and
    • raise (and log) any errors resulting from such queries in a consistent way

    Without commenting on the actual quality of this code, this is a pretty common practice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search