function test_dataOut() { $input_data = array(array("col_1" => 1, "col_2" => "1", "col_3" => 17384, "col_4" => 29411, "col_5" => 4871312, "col_6" => "43.71175", "col_7" => 32871, "col_8" => "test_string", "col_9" => array("val_1" => 1, "val_2" => "test_01", "val_3" => 17.239), "col_10" => array("val_1" => (string) 1, "val_2" => (string) -23580, "val_3" => (string) 17.239), "col_11" => array("val_1" => (double) 2, "val_2" => (double) -27450, "val_3" => (double) -26.17239), "col_12" => (int) mktime(0, 0, 0, 11, 21, 2010), "col_13" => (int) mktime(18, 44, 52, 11, 21, 2010), "col_14" => array('lat' => 23.417, 'lon' => 17.449))); // Set the disable_typecast flags to prevent FOX_db from automatically typecasting data // written to / read from the datyabase $this->tdb->disable_typecast_write = true; $this->tdb->disable_typecast_read = true; // Load the test data into the database. Because $disable_typecast_write is set it will be // stored *exactly* as sent in to the db try { $result = $this->tdb->runInsertQueryMulti(self::$struct, $input_data, $columns = null, $ctrl = null); } catch (FOX_exception $child) { $this->fail($child->dumpString(array('depth' => 50, 'data' => true))); } $this->assertEquals(1, $result, 'runInsertQueryMulti() reported adding wrong number of rows'); // Get the "types" array for the test data by running the query's "builder" function and extracting it from the result try { $result = $this->tdb->builder->buildSelectQueryCol(self::$struct, $col = 'col_1', $op = "=", $val = '1', $columns = null, $ctrl = array("format" => "row_object")); } catch (FOX_exception $child) { $this->fail($child->dumpString(array('depth' => 50, 'data' => true))); } $types = $result["types"]; // Fetch the test data into the database. Because $disable_typecast_read is set it will be // returned *exactly* as stored in the db try { $sql_result = $this->tdb->runSelectQueryCol(self::$struct, $col = 'col_1', $op = "=", $val = '1', $columns = null, $ctrl = array("format" => "row_object")); } catch (FOX_exception $child) { $this->fail($child->dumpString(array('depth' => 50, 'data' => true))); } // Run the returned test data through the query result typecaster, then compare the result to the check array. $check_data = new stdClass(); $check_data->col_1 = true; $check_data->col_2 = true; $check_data->col_3 = (int) 17384; $check_data->col_4 = (int) 29411; $check_data->col_5 = (double) 4871312; $check_data->col_6 = (double) 43.71175; $check_data->col_7 = (string) "32871"; $check_data->col_8 = (string) "test_string"; $check_data->col_9 = array("val_1" => 1, "val_2" => "test_01", "val_3" => 17.239); $check_data->col_10 = array("val_1" => (string) 1, "val_2" => (string) -23580, "val_3" => (string) 17.239); $check_data->col_11 = array("val_1" => (double) 2, "val_2" => (double) -27450, "val_3" => (double) -26.17239); $check_data->col_12 = (int) mktime(0, 0, 0, 11, 21, 2010); $check_data->col_13 = (int) mktime(18, 44, 52, 11, 21, 2010); $check_data->col_14 = array('lat' => 23.417, 'lon' => 17.449); $cst = new FOX_cast(); try { $result = $cst->queryResult($format = "row_object", $sql_result, $types); } catch (FOX_exception $child) { $this->fail($child->dumpString(1)); } $this->assertEquals($check_data, $result); }
/** * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. * * @version 1.0 * @since 1.0 * * @param array $args | Query args array * => VAL @param string [0] | First key in array is query string in vsprintf() format * => VAL @param mixed [N] | Each successive key is a var referred to in the query string * * @return string | Prepared query string */ function prepare($query, $params = null) { // Force floats to be locale unaware $query = preg_replace('|(?<!%)%f|', '%F', $query); // Quote the strings, avoiding escaped strings like %%s $query = preg_replace('|(?<!%)%s|', "'%s'", $query); // Replace our %r raw string token with an unquoted %s $query = preg_replace('|(?<!%)%r|', "%s", $query); $escaped_params = array(); if ($params) { $cast = new FOX_cast(); foreach ($params as $param) { if (!FOX_sUtil::keyExists('escape', $param) || !FOX_sUtil::keyExists('val', $param) || !FOX_sUtil::keyExists('php', $param) || !FOX_sUtil::keyExists('sql', $param)) { $text = "SAFETY INTERLOCK TRIP [ANTI SQL-INJECTION] - All data objects passed to the "; $text .= "database driver must include 'val', 'escape', 'php', and 'sql' parameters. This "; $text .= "interlock cannot be disabled."; throw new FOX_exception(array('numeric' => 1, 'text' => $text, 'data' => $param, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } try { $cast_val = $cast->PHPToSQL($param['val'], $param['php'], $param['sql']); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 2, 'text' => "Error while casting parameter", 'data' => array("val" => $param['val'], "php" => $param['php'], "sql" => $param['sql']), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => $child)); } if ($param['escape'] !== false) { // NOTE: parameters are in reverse order from mysqli_real_escape_string() $escaped_params[] = mysql_real_escape_string($cast_val, $this->dbh); } else { $escaped_params[] = $cast_val; } } unset($param); } $result = vsprintf($query, $escaped_params); return $result; }
/** * Builds an indate [INsert-upDATE] query for processing by $wpdb->prepare(). * * @version 1.0 * @since 1.0 * * @param array $struct | Structure of the db table, @see class FOX_db header for examples * * @param array/object $data | Class with $column_1, $column_2 in the namespace, or array of the form ("column_1"=>"value_1", "column_2"=>"value_2") * => KEY @param string | Name of the db column this key describes * => VAL @param int/string | Value to assign to the column * * @param bool/array $columns | Columns to use in query. NULL to select all columns. * => VAL @param string $mode | Column operating mode. "include" | "exclude" * => VAL @param string/array $col | Single column name as string. Multiple column names as array of strings * * @return array | Exception on failure. Query array on success. */ public function buildIndateQuery($struct, $data, $columns = null) { // Switch between unit test mode (pass as array) and // normal mode (pass as class name) // ==================================================== if (is_string($struct)) { $struct = call_user_func(array($struct, '_struct')); } // ==================================================== $params_list = array(); $columns_list = array(); $columns_list = array_keys($struct["columns"]); // Handle data passed as array where the array has missing // or nonexistent db column names // ===================================================== if (is_array($data)) { $columns_list = array_intersect($columns_list, array_keys($data)); } // Include or exclude one or more columns from the query // ###################################################### if ($columns != null) { if (!is_array($columns["col"])) { // Handle single column name as string $temp = array(); $temp[0] = $columns["col"]; $columns["col"] = $temp; } if ($columns["mode"] == "include") { $column_names = array_intersect($columns_list, $columns["col"]); } elseif ($columns["mode"] == "exclude") { $column_names = array_diff($columns_list, $columns["col"]); } } else { $column_names = $columns_list; } // Build the INSERT columns string // ###################################################### $columns_count = count($column_names) - 1; $columns_left = $columns_count; foreach ($column_names as $column_name) { $insert_columns_string .= $column_name; if ($columns_left != 0) { $insert_columns_string .= ", "; $columns_left--; } } // CASE 1 - Array Mode // ============================== if (is_array($data)) { $cast = new FOX_cast(); $query_formats .= "("; $columns_left = $columns_count; foreach ($column_names as $column_name) { $query_formats .= $struct["columns"][$column_name]["format"]; $in_type = $struct["columns"][$column_name]["php"]; $out_type = $struct["columns"][$column_name]["sql"]; if ($struct["columns"][$column_name]["format"] == "%r") { $escape = false; } else { $escape = true; } $params_list[] = array('escape' => $escape, 'val' => $cast->PHPToSQL($data[$column_name], $in_type, $out_type)); if ($columns_left != 0) { $query_formats .= ", "; $columns_left--; } } $query_formats .= ")"; } else { $columns_left = $columns_count; $cast = new FOX_cast(); $query_formats .= "("; foreach ($column_names as $column_name) { $query_formats .= $struct["columns"][$column_name]["format"]; $in_type = $struct["columns"][$column_name]["php"]; $out_type = $struct["columns"][$column_name]["sql"]; if ($struct["columns"][$column_name]["format"] == "%r") { $escape = false; } else { $escape = true; } $params_list[] = array('escape' => $escape, 'val' => $cast->PHPToSQL($data->{$column_name}, $in_type, $out_type)); if ($columns_left != 0) { $query_formats .= ", "; $columns_left--; } } $query_formats .= ")"; } // Build the UPDATE columns string // ###################################################### $columns_left = $columns_count; foreach ($column_names as $column_name) { $update_columns_string .= $column_name . " = " . $struct["columns"][$column_name]["format"]; $in_type = $struct["columns"][$column_name]["php"]; $out_type = $struct["columns"][$column_name]["sql"]; $cast = new FOX_cast(); if ($struct["columns"][$column_name]["format"] == "%r") { $escape = false; } else { $escape = true; } if (is_array($data)) { // Handle data passed as array $params_list[] = array('escape' => $escape, 'val' => $cast->PHPToSQL($data[$column_name], $in_type, $out_type)); } else { // Handle data passed as object $params_list[] = array('escape' => $escape, 'val' => $cast->PHPToSQL($data->{$column_name}, $in_type, $out_type)); } if ($columns_left != 0) { $update_columns_string .= ", "; $columns_left--; } } $query = "INSERT INTO " . $this->base_prefix . $struct["table"] . " (" . $insert_columns_string . ") VALUES " . $query_formats . " ON DUPLICATE KEY UPDATE " . $update_columns_string; // Merge all return data into an array // ############################################################### $result = array('query' => $query, 'params' => $params_list); return $result; }
/** * Runs a query on the database. Returns results in a specific format. * * @version 1.0 * @since 1.0 * @link https://github.com/FoxFire/foxfirewiki/DOCS_FOX_db_select_formatter * * @param string $sql | SQL query string * * @param array $ctrl | Control parameters for the query * => VAL @param string $format | Return format for query: "col", "row", "var", "array_key_object", "array_key_array" * "array_key_single", "array_object", "array_array", "raw", or (null) * @see result formatter headers inside this method for detailed docs * * => VAL @see FOX_db::runSelectQuery() and FOX_db::runSelectQueryJoin() for docs on remaining $ctrl options * * @return bool | Exception on failure. Query results array on success. */ public function runQuery($query, $ctrl = null) { $ctrl_default = array('format' => 'raw'); $ctrl = FOX_sUtil::parseArgs($ctrl, $ctrl_default); if ($this->print_query_args == true) { ob_start(); print_r($query); print_r($ctrl); $out = ob_get_clean(); FOX_debug::addToFile($out); } // Handle single parameter as string/int, or multiple // parameters passed as an array // ================================================== if (is_array($query)) { $sql = $this->driver->prepare($query['query'], $query['params']); } else { $sql = $query; $query = array('types' => array()); $ctrl = array("format" => "raw"); } // TODO: This prevents PHP from throwing a warning in queryResult() when the table // builder methods run their queries (which have no return type), but it should be // improved to prevent queries that are accidentally missing return data types // from leaking through if (!array_key_exists('types', $query)) { $query['types'] = array(); } if ($this->print_query_sql == true) { FOX_debug::addToFile($sql); } // EXAMPLE TABLE "test_table" // ================================= // col_1 | col_2 | col_3 | col_4 | // ================================= // 1 | red | dog | big | // 2 | green| cat | med | // 3 | blue | bird | small | // 4 | black| fish | tiny | $cast = new FOX_cast(); switch ($ctrl["format"]) { // VAR // ============================================================================= // Used when fetching query results that return a single variable. // // EXAMPLE: "SELECT COUNT(*) FROM test_table WHERE col_2 = red" // RESULT: string "1" (all data is returned in string format) case "var": try { $sql_result = $this->driver->get_var($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 1, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "\nRAW, format = var\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $result = $sql_result; } else { $result = $cast->queryResult($format = "var", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = var\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } } break; // COL // ============================================================================= // Used when fetching query results which only contain values from a single column. // // EXAMPLE: "SELECT col2 FROM test_table" // RESULT: array("red", "green", "blue", "black") // COL // ============================================================================= // Used when fetching query results which only contain values from a single column. // // EXAMPLE: "SELECT col2 FROM test_table" // RESULT: array("red", "green", "blue", "black") case "col": try { $sql_result = $this->driver->get_col($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 2, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "\nRAW, format = col\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $result = $sql_result; } else { $result = $cast->queryResult($format = "col", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = col\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } } break; // ROW_OBJECT // ============================================================================= // Returns a single database row as an object, with variable names mapped to // column names. If the query returns multiple rows, only the first row is returned. // // EXAMPLE: "SELECT * FROM test_table WHERE col_1 = 2" // RESULT: object stdClass( // col_1->2 // col_2->"green" // col_3->"cat" // col_4->"med" // ) // ROW_OBJECT // ============================================================================= // Returns a single database row as an object, with variable names mapped to // column names. If the query returns multiple rows, only the first row is returned. // // EXAMPLE: "SELECT * FROM test_table WHERE col_1 = 2" // RESULT: object stdClass( // col_1->2 // col_2->"green" // col_3->"cat" // col_4->"med" // ) case "row_object": try { $sql_result = $this->driver->get_row($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 3, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "\nRAW, format = row\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $result = $sql_result; } else { $result = $cast->queryResult($format = "row_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = row_object\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } } break; // ROW_ARRAY // ============================================================================= // Returns a single database row as an array, with key names mapped to column // names. If the query returns multiple rows, only the first row is returned. // // EXAMPLE: "SELECT * FROM test_table WHERE col_1 = 2" // RESULT: array( // "col_1"=>2 // "col_2"=>"green" // "col_3"=>"cat" // "col_4"=>"med" // ) // ROW_ARRAY // ============================================================================= // Returns a single database row as an array, with key names mapped to column // names. If the query returns multiple rows, only the first row is returned. // // EXAMPLE: "SELECT * FROM test_table WHERE col_1 = 2" // RESULT: array( // "col_1"=>2 // "col_2"=>"green" // "col_3"=>"cat" // "col_4"=>"med" // ) case "row_array": try { $sql_result = $this->driver->get_row($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 4, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "\nRAW, format = row_array\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "row_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = row_array\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); // Convert row object into array foreach ($data as $key => $value) { $result[$key] = $value; } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = row_array\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_OBJECT // ============================================================================= // // When used with a single column name passed as a string: // // Returns results as an array of objects, where the primary array key names are // set based on the contents of a database column. // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"]="col_3" // RESULT: array( // "dog" =>stdClass( "col_1"->1, "col_2"->"red", "col_3"->"dog", "col_4"->"big"), // "cat" =>stdClass( "col_1"->2, "col_2"->"green", "col_3"->"cat", "col_4"->"med"), // "bird"=>stdClass( "col_1"->3, "col_2"->"blue", "col_3"->"bird", "col_4"->"small"), // "fish"=>stdClass( "col_1"->4, "col_2"->"black", "col_3"->"fish", "col_4"->"tiny"), // ) // When used with multiple column names passed as an array of strings: // // Returns results as an array of arrays^N, where the array key names and heirarchy // are set based on the contents of the $key_col array. This output format REQUIRES // that each taxonomy group (in the example below "col_3" + "col_4") is UNIQUE // // EXAMPLE TABLE "test_table_2" // ====================================== // col_1 | col_2 | col_3 | col_4 | col_5 // ====================================== // 1 | red | dog | big | heavy // 2 | green| dog | med | light // 3 | blue | bird | small | light // 4 | black| fish | tiny | average // 5 | black| fish | large | light // // UNIQUE KEY col_3_col4(col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_3, "col_4") // RESULT: array( // "dog" =>array( // "big"=>stdClass("col_1"->1, "col_2"->"red", "col_5"->"heavy"), // "med"=>stdClass("col_1"->2, "col_2"->"green", "col_5"->"light") // ), // "bird"=>array( // "small"=>stdClass("col_1"->3, "col_2"->"blue", "col_5"->"light") // ), // "fish"=>array( // "tiny"=>stdClass("col_1"->4, "col_2"->"black", "col_5"->"average"), // "large"=>stdClass("col_1"->5, "col_2"->"black", "col_5"->"light") // ) // ) // ARRAY_KEY_OBJECT // ============================================================================= // // When used with a single column name passed as a string: // // Returns results as an array of objects, where the primary array key names are // set based on the contents of a database column. // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"]="col_3" // RESULT: array( // "dog" =>stdClass( "col_1"->1, "col_2"->"red", "col_3"->"dog", "col_4"->"big"), // "cat" =>stdClass( "col_1"->2, "col_2"->"green", "col_3"->"cat", "col_4"->"med"), // "bird"=>stdClass( "col_1"->3, "col_2"->"blue", "col_3"->"bird", "col_4"->"small"), // "fish"=>stdClass( "col_1"->4, "col_2"->"black", "col_3"->"fish", "col_4"->"tiny"), // ) // When used with multiple column names passed as an array of strings: // // Returns results as an array of arrays^N, where the array key names and heirarchy // are set based on the contents of the $key_col array. This output format REQUIRES // that each taxonomy group (in the example below "col_3" + "col_4") is UNIQUE // // EXAMPLE TABLE "test_table_2" // ====================================== // col_1 | col_2 | col_3 | col_4 | col_5 // ====================================== // 1 | red | dog | big | heavy // 2 | green| dog | med | light // 3 | blue | bird | small | light // 4 | black| fish | tiny | average // 5 | black| fish | large | light // // UNIQUE KEY col_3_col4(col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_3, "col_4") // RESULT: array( // "dog" =>array( // "big"=>stdClass("col_1"->1, "col_2"->"red", "col_5"->"heavy"), // "med"=>stdClass("col_1"->2, "col_2"->"green", "col_5"->"light") // ), // "bird"=>array( // "small"=>stdClass("col_1"->3, "col_2"->"blue", "col_5"->"light") // ), // "fish"=>array( // "tiny"=>stdClass("col_1"->4, "col_2"->"black", "col_5"->"average"), // "large"=>stdClass("col_1"->5, "col_2"->"black", "col_5"->"light") // ) // ) case "array_key_object": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 5, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "\nRAW, format = array_key_object\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_object\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); // If a single column name is passed as a string, use the more efficient // direct assignment algorithm to build a 1 level tree if (!is_array($ctrl["key_col"])) { foreach ($data as $row) { $key = $row->{$ctrl["key_col"]}; $result[$key] = $row; } } else { foreach ($data as $row) { // Since there is no functionality in PHP for creating a new array key // based on a name stored in a variable ( $$ variable variable syntax does // not work for multidimensional arrays, we have to build a string of PHP // code and use eval() to run it $eval_str = "\$result"; foreach ($ctrl["key_col"] as $keyname) { $eval_str .= '["' . $row->{$keyname} . '"]'; } $eval_str .= " = \$row_copy;"; $row_copy = new stdClass(); // Copy the row object into a new stdClass, skipping keys that are used as the // branch variables foreach ($row as $key => $value) { if (array_search($key, $ctrl["key_col"]) === false) { $row_copy->{$key} = $value; } } // Run the PHP string we have built eval($eval_str); } } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_key_object\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_ARRAY // ============================================================================= // When used with a single column name passed as a string: // // Returns results as an array of arrays, where the primary array key names are // set based on the contents of a database column. // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"]="col_3" // RESULT: array( // "dog" =>array( "col_1"=>1, "col_2"=>"red", "col_3"=>"dog", "col_4"=>"big"), // "cat" =>array( "col_1"=>2, "col_2"=>"green", "col_3"=>"cat", "col_4"=>"med"), // "bird"=>array( "col_1"=>3, "col_2"=>"blue", "col_3"=>"bird", "col_4"=>"small"), // "fish"=>array( "col_1"=>4, "col_2"=>"black", "col_3"=>"fish", "col_4"=>"tiny"), // ) // // When used with multiple column names passed as an array of strings: // // Returns results as an array of arrays^N, where the array key names and heirarchy // are set based on the contents of the $key_col array. This output format REQUIRES // that each taxonomy group (in the example below "col_3" + "col_4") is UNIQUE // // EXAMPLE TABLE "test_table_2" // ====================================== // col_1 | col_2 | col_3 | col_4 | col_5 // ====================================== // 1 | red | dog | big | heavy // 2 | green| dog | med | light // 3 | blue | bird | small | light // 4 | black| fish | tiny | average // 5 | black| fish | large | light // // UNIQUE KEY col_3_col4(col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_3, "col_4") // RESULT: array( // "dog" =>array( // "big"=>array("col_1"=>1, "col_2"=>"red", "col_5"=>"heavy"), // "med"=>array("col_1"=>2, "col_2"=>"green", "col_5"=>"light") // ), // "bird"=>array( // "small"=>array("col_1"=>3, "col_2"=>"blue", "col_5"=>"light") // ), // "fish"=>array( // "tiny"=>array("col_1"=>4, "col_2"=>"black", "col_5"=>"average"), // "large"=>array("col_1"=>5, "col_2"=>"black", "col_5"=>"light") // ) // ) // ARRAY_KEY_ARRAY // ============================================================================= // When used with a single column name passed as a string: // // Returns results as an array of arrays, where the primary array key names are // set based on the contents of a database column. // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"]="col_3" // RESULT: array( // "dog" =>array( "col_1"=>1, "col_2"=>"red", "col_3"=>"dog", "col_4"=>"big"), // "cat" =>array( "col_1"=>2, "col_2"=>"green", "col_3"=>"cat", "col_4"=>"med"), // "bird"=>array( "col_1"=>3, "col_2"=>"blue", "col_3"=>"bird", "col_4"=>"small"), // "fish"=>array( "col_1"=>4, "col_2"=>"black", "col_3"=>"fish", "col_4"=>"tiny"), // ) // // When used with multiple column names passed as an array of strings: // // Returns results as an array of arrays^N, where the array key names and heirarchy // are set based on the contents of the $key_col array. This output format REQUIRES // that each taxonomy group (in the example below "col_3" + "col_4") is UNIQUE // // EXAMPLE TABLE "test_table_2" // ====================================== // col_1 | col_2 | col_3 | col_4 | col_5 // ====================================== // 1 | red | dog | big | heavy // 2 | green| dog | med | light // 3 | blue | bird | small | light // 4 | black| fish | tiny | average // 5 | black| fish | large | light // // UNIQUE KEY col_3_col4(col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_3, "col_4") // RESULT: array( // "dog" =>array( // "big"=>array("col_1"=>1, "col_2"=>"red", "col_5"=>"heavy"), // "med"=>array("col_1"=>2, "col_2"=>"green", "col_5"=>"light") // ), // "bird"=>array( // "small"=>array("col_1"=>3, "col_2"=>"blue", "col_5"=>"light") // ), // "fish"=>array( // "tiny"=>array("col_1"=>4, "col_2"=>"black", "col_5"=>"average"), // "large"=>array("col_1"=>5, "col_2"=>"black", "col_5"=>"light") // ) // ) case "array_key_array": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 6, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_key_array\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_array\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); // If a single column name is passed as a string, use the more efficient // direct assignment algorithm to build a 1 level tree if (!is_array($ctrl["key_col"])) { foreach ($data as $row) { $arr = array(); // Convert row object into array foreach ($row as $key => $value) { $arr[$key] = $value; } // Insert row array into primary array as named key $result[$row->{$ctrl["key_col"]}] = $arr; } } else { foreach ($data as $row) { // Since there is no functionality in PHP for creating a new array key // based on a name stored in a variable ( $$ variable variable syntax does // not work for multidimensional arrays), we have to build a string of PHP // code and use eval() to run it $eval_str = "\$result"; foreach ($ctrl["key_col"] as $keyname) { $eval_str .= '["' . $row->{$keyname} . '"]'; } $eval_str .= " = \$arr;"; $arr = array(); // Convert row object into array, skipping keys that are used as the // branch variables foreach ($row as $key => $value) { // Check if this is a first-order row. If it is, "lift" it up a // level in the results array to avoid adding an unnecessary "L0" // wrapper array around it $order = count(array_keys((array) $row)) - count($ctrl["key_col"]); if ($order > 1) { if (array_search($key, $ctrl["key_col"]) === false) { $arr[$key] = $value; } } else { $arr = $value; } } // Run the PHP string we have built eval($eval_str); } } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_key_array\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_ARRAY_GROUPED // ============================================================================= // // Requires at least TWO column names, and columns not specified in $key_col are not // included in the results set. Returns results as an array of arrays^N-1, where the // array key names and heirarchy are set based on the contents of the $key_col array. // Results in the last db column specified in $key_col are grouped together in an // int-keyed array. The key name corresponds to the order in which a rows is returned // from the database and the value of each key is the column's value in the database // row. For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_grouped() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"), // "green"=> array("B","C","D") // ), // "bird" =>array( // "blue"=> array("A"), // "black"=> array("A") // ), // "fish" =>array( // "black"=> array("A","B") // ) // ) // ARRAY_KEY_ARRAY_GROUPED // ============================================================================= // // Requires at least TWO column names, and columns not specified in $key_col are not // included in the results set. Returns results as an array of arrays^N-1, where the // array key names and heirarchy are set based on the contents of the $key_col array. // Results in the last db column specified in $key_col are grouped together in an // int-keyed array. The key name corresponds to the order in which a rows is returned // from the database and the value of each key is the column's value in the database // row. For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_grouped() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"), // "green"=> array("B","C","D") // ), // "bird" =>array( // "blue"=> array("A"), // "black"=> array("A") // ), // "fish" =>array( // "black"=> array("A","B") // ) // ) case "array_key_array_grouped": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 7, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_key_array_grouped\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_array_grouped\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { // Since there is no functionality in PHP for creating a new array key // based on a name stored in a variable ( $$ variable variable syntax does // not work for multidimensional arrays), we have to build a string of PHP // code and use eval() to run it $eval_str = "\$result"; $idx = sizeof($ctrl["key_col"]) - 1; $grouped_col = $ctrl["key_col"][$idx]; foreach ($ctrl["key_col"] as $keyname) { if ($keyname != $grouped_col) { $eval_str .= '["' . $row->{$keyname} . '"]'; } else { $eval_str .= '[]'; } } $eval_str .= " = \$row->" . $grouped_col . ";"; // Run the PHP string we have built eval($eval_str); } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_key_array_grouped\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_ARRAY_TRUE // ============================================================================= // // Returns results as an array of arrays^N-1, where the array key names and heirarchy // are set based on the contents of the $key_col array. Results in the last db column // specified in $key_col are grouped together in an result-keyed array where the key // name is the column's value in the database row and the key's value is (bool)true. // For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_true() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"=>"true"), // "green"=> array("B"=>"true","C"=>"true","D"=>"true") // ), // "bird" =>array( // "blue"=> array("A"=>"true"), // "black"=> array("A"=>"true") // ), // "fish" =>array( // "black"=> array("A"=>"true","B"=>"true") // ) // ) // ARRAY_KEY_ARRAY_TRUE // ============================================================================= // // Returns results as an array of arrays^N-1, where the array key names and heirarchy // are set based on the contents of the $key_col array. Results in the last db column // specified in $key_col are grouped together in an result-keyed array where the key // name is the column's value in the database row and the key's value is (bool)true. // For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_true() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"=>"true"), // "green"=> array("B"=>"true","C"=>"true","D"=>"true") // ), // "bird" =>array( // "blue"=> array("A"=>"true"), // "black"=> array("A"=>"true") // ), // "fish" =>array( // "black"=> array("A"=>"true","B"=>"true") // ) // ) case "array_key_array_true": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 8, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_key_array_true\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_array_true\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { // Since there is no functionality in PHP for creating a new array key // based on a name stored in a variable ( $$ variable variable syntax does // not work for multidimensional arrays), we have to build a string of PHP // code and use eval() to run it $eval_str = "\$result"; foreach ($ctrl["key_col"] as $keyname) { $eval_str .= '["' . $row->{$keyname} . '"]'; } $eval_str .= " = true;"; // Run the PHP string we have built eval($eval_str); } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_key_array_true\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_ARRAY_FALSE // ============================================================================= // // Returns results as an array of arrays^N-1, where the array key names and heirarchy // are set based on the contents of the $key_col array. Results in the last db column // specified in $key_col are grouped together in an result-keyed array where the key // name is the column's value in the database row and the key's value is (bool)false. // For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_false() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"=>"false"), // "green"=> array("B"=>"false","C"=>"false","D"=>"false") // ), // "bird" =>array( // "blue"=> array("A"=>"false"), // "black"=> array("A"=>"false") // ), // "fish" =>array( // "black"=> array("A"=>"false","B"=>"false") // ) // ) // ARRAY_KEY_ARRAY_FALSE // ============================================================================= // // Returns results as an array of arrays^N-1, where the array key names and heirarchy // are set based on the contents of the $key_col array. Results in the last db column // specified in $key_col are grouped together in an result-keyed array where the key // name is the column's value in the database row and the key's value is (bool)false. // For a working example of how to use this result formatter, see the function // database_resultFormatters::test_array_key_array_false() in the database unit tests. // // EXAMPLE TABLE "test_table_2" // =============================== // col_1 | col_2 | col_3 | col_4 // =============================== // 1 | red | dog | A // 2 | green| dog | B // 3 | green| dog | C // 4 | green| dog | D // 5 | blue | bird | A // 6 | black| bird | A // 7 | black| fish | A // 8 | black| fish | B // // UNIQUE KEY no_duplicates(col_2, col_3, col_4) // // EXAMPLE: "SELECT * FROM test_table" + $ctrl["key_col"] = array("col_2, "col_3", "col_4") // NOTE: "col_1" is excluded because it is not specified in $ctrl["key_col"] // RESULT: array( // "dog" =>array( // "red"=> array("A"=>"false"), // "green"=> array("B"=>"false","C"=>"false","D"=>"false") // ), // "bird" =>array( // "blue"=> array("A"=>"false"), // "black"=> array("A"=>"false") // ), // "fish" =>array( // "black"=> array("A"=>"false","B"=>"false") // ) // ) case "array_key_array_false": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 9, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_key_array_false"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_array_false"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { // Since there is no functionality in PHP for creating a new array key // based on a name stored in a variable ( $$ variable variable syntax does // not work for multidimensional arrays), we have to build a string of PHP // code and use eval() to run it $eval_str = "\$result"; foreach ($ctrl["key_col"] as $keyname) { $eval_str .= '["' . $row->{$keyname} . '"]'; } $eval_str .= " = false;"; // Run the PHP string we have built eval($eval_str); } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_key_array_false"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_KEY_SINGLE // ============================================================================= // Returns results as an array of ints or strings, where the array key names // are set based on the contents of a database column. // // EXAMPLE: "SELECT col_2, col_3 FROM test_table" + $ctrl["key_col"]="col_2", $ctrl["val_col"]="col_3" // RESULT: array( // "red"=>"dog", // "green"=>"cat", // "blue"=>"bird", // "black"=>"fish" // ) // ARRAY_KEY_SINGLE // ============================================================================= // Returns results as an array of ints or strings, where the array key names // are set based on the contents of a database column. // // EXAMPLE: "SELECT col_2, col_3 FROM test_table" + $ctrl["key_col"]="col_2", $ctrl["val_col"]="col_3" // RESULT: array( // "red"=>"dog", // "green"=>"cat", // "blue"=>"bird", // "black"=>"fish" // ) case "array_key_single": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 10, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_key_single\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_key_single\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { $result[$row->{$ctrl["key_col"]}] = $row->{$ctrl["val_col"]}; } if ($this->print_result_formatted == true) { ob_start(); echo "\nCAST, format = array_key_single\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_OBJECT // ============================================================================= // Returns results as an array of objects, where the primary array keys are // zero-indexed ints // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // stdClass( "col_1"->1, "col_2"->"red", "col_3"->"dog", "col_4"->"big"), // stdClass( "col_1"->2, "col_2"->"green", "col_3"->"cat", "col_4"->"med"), // stdClass( "col_1"->3, "col_2"->"blue", "col_3"->"bird", "col_4"->"small"), // stdClass( "col_1"->4, "col_2"->"black", "col_3"->"fish", "col_4"->"tiny"), // ) // ARRAY_OBJECT // ============================================================================= // Returns results as an array of objects, where the primary array keys are // zero-indexed ints // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // stdClass( "col_1"->1, "col_2"->"red", "col_3"->"dog", "col_4"->"big"), // stdClass( "col_1"->2, "col_2"->"green", "col_3"->"cat", "col_4"->"med"), // stdClass( "col_1"->3, "col_2"->"blue", "col_3"->"bird", "col_4"->"small"), // stdClass( "col_1"->4, "col_2"->"black", "col_3"->"fish", "col_4"->"tiny"), // ) case "array_object": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 11, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_object\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $result = $sql_result; } else { $result = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_object\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($this->print_result_formatted == true) { ob_start(); print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } break; // ARRAY_ARRAY // ============================================================================= // Returns results as an array of arrays, where the primary array keys are // zero-indexed ints // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // array( "col_1"=>1, "col_2"=>"red", "col_3"=>"dog", "col_4"=>"big"), // array( "col_1"=>2, "col_2"=>"green", "col_3"=>"cat", "col_4"=>"med"), // array( "col_1"=>3, "col_2"=>"blue", "col_3"=>"bird", "col_4"=>"small"), // array( "col_1"=>4, "col_2"=>"black", "col_3"=>"fish", "col_4"=>"tiny"), // ) // ARRAY_ARRAY // ============================================================================= // Returns results as an array of arrays, where the primary array keys are // zero-indexed ints // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // array( "col_1"=>1, "col_2"=>"red", "col_3"=>"dog", "col_4"=>"big"), // array( "col_1"=>2, "col_2"=>"green", "col_3"=>"cat", "col_4"=>"med"), // array( "col_1"=>3, "col_2"=>"blue", "col_3"=>"bird", "col_4"=>"small"), // array( "col_1"=>4, "col_2"=>"black", "col_3"=>"fish", "col_4"=>"tiny"), // ) case "array_array": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 12, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_array\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_array\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { $arr = array(); // Convert row object into array foreach ($row as $key => $value) { $arr[$key] = $value; } // Insert row array into primary array as unnamed key $result[] = $arr; } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_array\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // ARRAY_ARRAY_OFFSET // ============================================================================= // Returns results as an array of arrays, where the primary array keys are // zero-indexed ints, and the secondary array keys are the offset of the // column in the table definition array // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // array( 1, "red", "dog", "big"), // array( 2, "green", "cat", "med"), // array( 3, "blue", "bird", "small"), // array( 4, "black", "fish", "tiny"), // ) // ARRAY_ARRAY_OFFSET // ============================================================================= // Returns results as an array of arrays, where the primary array keys are // zero-indexed ints, and the secondary array keys are the offset of the // column in the table definition array // // EXAMPLE: "SELECT * FROM test_table" // RESULT: array( // array( 1, "red", "dog", "big"), // array( 2, "green", "cat", "med"), // array( 3, "blue", "bird", "small"), // array( 4, "black", "fish", "tiny"), // ) case "array_array_offset": try { $sql_result = $this->driver->get_results($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 12, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "RAW, format = array_array_offset\n"; print_r($sql_result); $out = ob_get_clean(); FOX_debug::addToFile($out); } if ($this->disable_typecast_read == true) { $data = $sql_result; } else { $data = $cast->queryResult($format = "array_object", $sql_result, $query["types"]); if ($this->print_result_cast == true) { ob_start(); echo "\nCAST, format = array_array_offset\n"; print_r($data); $out = ob_get_clean(); FOX_debug::addToFile($out); } } if ($data) { $result = array(); foreach ($data as $row) { $arr = array(); // Convert row object into array foreach ($row as $key => $value) { $arr[] = $value; } // Insert row array into primary array as unnamed key $result[] = $arr; } if ($this->print_result_formatted == true) { ob_start(); echo "\nFORMATTED, format = array_array_offset\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } unset($data); // Reduce memory usage } break; // RAW // ============================================================================= // Runs a default SQL query. Returned result format depends on the query. // RAW // ============================================================================= // Runs a default SQL query. Returned result format depends on the query. case "raw": try { $result = $this->driver->query($sql); } catch (FOX_exception $child) { throw new FOX_exception(array('numeric' => 13, 'text' => "Error in database driver", 'data' => array('query' => $query, 'sql' => $sql), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child)); } if ($this->print_result_raw == true) { ob_start(); echo "format = null\n"; print_r($result); $out = ob_get_clean(); FOX_debug::addToFile($out); } break; default: throw new FOX_exception(array('numeric' => 14, 'text' => "Invalid query runner format", 'data' => array('faulting_format' => $ctrl["format"], "query" => $query, "ctrl" => $ctrl), 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } // END switch($ctrl["format"]) return $result; }