function DatabaseHandler::db_query

7.x DatabaseHandler.php DatabaseHandler::db_query($sql_query, $args = array())
6.x DatabaseHandler.php DatabaseHandler::db_query($sql_query, $args = array())

This function is used to perform a database query. It uses PDO execute, which will take automatically replace ? with variables you supply as the arguments to this function, or as an array to this function. Either will work. Do this by using ?, or naming the variable like :name or :age.

For example: $result = $db->db_query("SELECT * FROM table WHERE name = ? and age = ? ", $name, $temp_age); or $result = $db->db_query("SELECT * FROM table WHERE name = ? AND age = ? ", array($name, $temp_age)); or $result = $db->db_query("SELECT * FROM table WHERE name = :name ", array(":name" => $name));

Parameters

unknown_type $sql_query:

Return value

unknown

33 calls to DatabaseHandler::db_query()
DatabaseHandler::add_draft_instruction in classes/DatabaseHandler.php
DatabaseHandler::duplicate_course_for_year in classes/DatabaseHandler.php
Note that the $course object need not be a Course object, but rather a simple stdClass() with certain properties set. Specifically, the values we see being input into draft_courses in this function.
DatabaseHandler::get_advising_session_id in classes/DatabaseHandler.php
DatabaseHandler::get_course_id in classes/DatabaseHandler.php
DatabaseHandler::get_degrees_in_catalog_year in classes/DatabaseHandler.php

... See full list

File

classes/DatabaseHandler.php, line 139

Class

DatabaseHandler

Code

function db_query($sql_query, $args = array()) {

  // If there were any arguments to this function, then we must first apply
  // replacement patterns.
  $args = func_get_args();

  array_shift($args);
  if (isset($args [0]) && is_array($args [0])) {
    // If the first argument was an array, it means we passed an array of values instead
    // of passing them directly.  So use them directly as our args.
    $args = $args [0];

    // If we were supplied an array, then we need to see if the NEW args[0] is an array...  If it is, grab the first element AGAIN.
    if (isset($args [0]) && is_array($args [0])) {
      $args = $args [0];
    }
  }


  // We need to make sure that arguments are passed without being contained in single quotes ('?').  Should be just ?
  $sql_query = str_replace("'?'", "?", $sql_query);

  // If $c (number of replacements performed) does not match the number of replacements
  // specified, warn the user.
  /*
     * Don't do this anymore, as it might throw off queries that don't use ?'s, but instead use :var  as the replacements.
     *
     if (substr_count($sql_query, "?") != count($args)) {
     fpm("<br><b>WARNING:</b> Replacement count does not match what was supplied to query: $sql_query<br><br>");
     }
     */

  //////////////////////////////////////////////

  // Run the sqlQuery and return the result set.
  if (!isset($this->pdo) || $this->pdo == NULL) {
    fpm(debug_backtrace());
  }


  try {

    $result = $this->pdo->prepare($sql_query);
    $result->execute($args);

    $_SESSION ["fp_last_insert_id"] = $this->pdo->lastInsertId(); // capture last insert id, in case we ask for it later.

    return $result;
  }
  catch (Exception $ex) {
    // Some error happened!
    $this->db_error($ex);
  }


}