function DatabaseHandler::db_query
Search API
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 - 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
File
- classes/
DatabaseHandler.php, line 175
Class
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);
}
/*
$result = mysql_query($sql_query, $this->dbc);
if ($result)
{
return $result;
} else {
// Meaning, the query failed...
// Do nothing. Do not attempt to log anything, as that could cause an infinite loop.
// Display the error on screen
$this->db_error();
}
**/
}