Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/flow-flo.../libs/colshrap.../safemysq...
File: safemysql.class.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* @author col.shrapnel@gmail.com
[2] Fix | Delete
* @link http://phpfaq.ru/safemysql
[3] Fix | Delete
*
[4] Fix | Delete
* Safe and convenient way to handle SQL queries utilizing type-hinted placeholders.
[5] Fix | Delete
*
[6] Fix | Delete
* Key features
[7] Fix | Delete
* - set of helper functions to get the desired result right out of query, like in PEAR::DB
[8] Fix | Delete
* - conditional query building using parse() method to build queries of whatever comlexity,
[9] Fix | Delete
* while keeping extra safety of placeholders
[10] Fix | Delete
* - type-hinted placeholders
[11] Fix | Delete
*
[12] Fix | Delete
* Type-hinted placeholders are great because
[13] Fix | Delete
* - safe, as any other [properly implemented] placeholders
[14] Fix | Delete
* - no need for manual escaping or binding, makes the code extra DRY
[15] Fix | Delete
* - allows support for non-standard types such as identifier or array, which saves A LOT of pain in the back.
[16] Fix | Delete
*
[17] Fix | Delete
* Supported placeholders at the moment are:
[18] Fix | Delete
*
[19] Fix | Delete
* ?s ("string") - strings (also DATE, FLOAT and DECIMAL)
[20] Fix | Delete
* ?i ("integer") - the name says it all
[21] Fix | Delete
* ?n ("name") - identifiers (table and field names)
[22] Fix | Delete
* ?a ("array") - complex placeholder for IN() operator (substituted with string of 'a','b','c' format, without parentesis)
[23] Fix | Delete
* ?u ("update") - complex placeholder for SET operator (substituted with string of `field`='value',`field`='value' format)
[24] Fix | Delete
* and
[25] Fix | Delete
* ?p ("parsed") - special type placeholder, for inserting already parsed statements without any processing, to avoid double parsing.
[26] Fix | Delete
*
[27] Fix | Delete
* Connection:
[28] Fix | Delete
*
[29] Fix | Delete
* $db = new SafeMySQL(); // with default settings
[30] Fix | Delete
*
[31] Fix | Delete
* $opts = array(
[32] Fix | Delete
* 'user' => 'user',
[33] Fix | Delete
* 'pass' => 'pass',
[34] Fix | Delete
* 'db' => 'db',
[35] Fix | Delete
* 'charset' => 'latin1'
[36] Fix | Delete
* );
[37] Fix | Delete
* $db = new SafeMySQL($opts); // with some of the default settings overwritten
[38] Fix | Delete
*
[39] Fix | Delete
* Alternatively, you can just pass an existing mysqli instance that will be used to run queries
[40] Fix | Delete
* instead of creating a new connection.
[41] Fix | Delete
* Excellent choice for migration!
[42] Fix | Delete
*
[43] Fix | Delete
* $db = new SafeMySQL(['mysqli' => $mysqli]);
[44] Fix | Delete
*
[45] Fix | Delete
* Some examples:
[46] Fix | Delete
*
[47] Fix | Delete
* $name = $db->getOne('SELECT name FROM table WHERE id = ?i',$_GET['id']);
[48] Fix | Delete
* $data = $db->getInd('id','SELECT * FROM ?n WHERE id IN ?a','table', array(1,2));
[49] Fix | Delete
* $data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);
[50] Fix | Delete
*
[51] Fix | Delete
* $ids = $db->getCol("SELECT id FROM tags WHERE tagname = ?s",$tag);
[52] Fix | Delete
* $data = $db->getAll("SELECT * FROM table WHERE category IN (?a)",$ids);
[53] Fix | Delete
*
[54] Fix | Delete
* $data = array('offers_in' => $in, 'offers_out' => $out);
[55] Fix | Delete
* $sql = "INSERT INTO stats SET pid=?i,dt=CURDATE(),?u ON DUPLICATE KEY UPDATE ?u";
[56] Fix | Delete
* $db->query($sql,$pid,$data,$data);
[57] Fix | Delete
*
[58] Fix | Delete
* if ($var === NULL) {
[59] Fix | Delete
* $sqlpart = "field is NULL";
[60] Fix | Delete
* } else {
[61] Fix | Delete
* $sqlpart = $db->parse("field = ?s", $var);
[62] Fix | Delete
* }
[63] Fix | Delete
* $data = $db->getAll("SELECT * FROM table WHERE ?p", $bar, $sqlpart);
[64] Fix | Delete
*
[65] Fix | Delete
*/
[66] Fix | Delete
[67] Fix | Delete
class SafeMySQL
[68] Fix | Delete
{
[69] Fix | Delete
[70] Fix | Delete
protected $conn;
[71] Fix | Delete
protected $stats;
[72] Fix | Delete
protected $emode;
[73] Fix | Delete
protected $exname;
[74] Fix | Delete
[75] Fix | Delete
protected $defaults = array(
[76] Fix | Delete
'host' => 'localhost',
[77] Fix | Delete
'user' => 'root',
[78] Fix | Delete
'pass' => '',
[79] Fix | Delete
'db' => 'test',
[80] Fix | Delete
'port' => NULL,
[81] Fix | Delete
'socket' => NULL,
[82] Fix | Delete
'pconnect' => FALSE,
[83] Fix | Delete
'charset' => 'utf8',
[84] Fix | Delete
'errmode' => 'exception', //or 'error'
[85] Fix | Delete
'exception' => 'Exception', //Exception class name
[86] Fix | Delete
);
[87] Fix | Delete
[88] Fix | Delete
const RESULT_ASSOC = MYSQLI_ASSOC;
[89] Fix | Delete
const RESULT_NUM = MYSQLI_NUM;
[90] Fix | Delete
[91] Fix | Delete
function __construct($opt = array())
[92] Fix | Delete
{
[93] Fix | Delete
$opt = array_merge($this->defaults,$opt);
[94] Fix | Delete
[95] Fix | Delete
$this->emode = $opt['errmode'];
[96] Fix | Delete
$this->exname = $opt['exception'];
[97] Fix | Delete
[98] Fix | Delete
if (isset($opt['mysqli']))
[99] Fix | Delete
{
[100] Fix | Delete
if ($opt['mysqli'] instanceof mysqli)
[101] Fix | Delete
{
[102] Fix | Delete
$this->conn = $opt['mysqli'];
[103] Fix | Delete
return;
[104] Fix | Delete
[105] Fix | Delete
} else {
[106] Fix | Delete
[107] Fix | Delete
$this->error("mysqli option must be valid instance of mysqli class");
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
if ($opt['pconnect'])
[112] Fix | Delete
{
[113] Fix | Delete
$opt['host'] = "p:".$opt['host'];
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
@$this->conn = mysqli_connect($opt['host'], $opt['user'], $opt['pass'], $opt['db'], $opt['port'], $opt['socket']);
[117] Fix | Delete
if ( !$this->conn )
[118] Fix | Delete
{
[119] Fix | Delete
$this->error(mysqli_connect_errno()." ".mysqli_connect_error());
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
mysqli_set_charset($this->conn, $opt['charset']) or $this->error(mysqli_error($this->conn));
[123] Fix | Delete
unset($opt); // I am paranoid
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Conventional function to run a query with placeholders. A mysqli_query wrapper with placeholders support
[128] Fix | Delete
*
[129] Fix | Delete
* Examples:
[130] Fix | Delete
* $db->query("DELETE FROM table WHERE id=?i", $id);
[131] Fix | Delete
*
[132] Fix | Delete
* @param string $query - an SQL query with placeholders
[133] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[134] Fix | Delete
* @return resource|FALSE whatever mysqli_query returns
[135] Fix | Delete
*/
[136] Fix | Delete
public function query()
[137] Fix | Delete
{
[138] Fix | Delete
return $this->rawQuery($this->prepareQuery(func_get_args()));
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Conventional function to fetch single row.
[143] Fix | Delete
*
[144] Fix | Delete
* @param resource $result - myqli result
[145] Fix | Delete
* @param int $mode - optional fetch mode, RESULT_ASSOC|RESULT_NUM, default RESULT_ASSOC
[146] Fix | Delete
* @return array|FALSE whatever mysqli_fetch_array returns
[147] Fix | Delete
*/
[148] Fix | Delete
public function fetch($result,$mode=self::RESULT_ASSOC)
[149] Fix | Delete
{
[150] Fix | Delete
return mysqli_fetch_array($result, $mode);
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Conventional function to get number of affected rows.
[155] Fix | Delete
*
[156] Fix | Delete
* @return int whatever mysqli_affected_rows returns
[157] Fix | Delete
*/
[158] Fix | Delete
public function affectedRows()
[159] Fix | Delete
{
[160] Fix | Delete
return mysqli_affected_rows ($this->conn);
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Conventional function to get last insert id.
[165] Fix | Delete
*
[166] Fix | Delete
* @return int whatever mysqli_insert_id returns
[167] Fix | Delete
*/
[168] Fix | Delete
public function insertId()
[169] Fix | Delete
{
[170] Fix | Delete
return mysqli_insert_id($this->conn);
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Conventional function to get number of rows in the resultset.
[175] Fix | Delete
*
[176] Fix | Delete
* @param resource $result - myqli result
[177] Fix | Delete
* @return int whatever mysqli_num_rows returns
[178] Fix | Delete
*/
[179] Fix | Delete
public function numRows($result)
[180] Fix | Delete
{
[181] Fix | Delete
return mysqli_num_rows($result);
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Conventional function to free the resultset.
[186] Fix | Delete
*/
[187] Fix | Delete
public function free($result)
[188] Fix | Delete
{
[189] Fix | Delete
mysqli_free_result($result);
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/**
[193] Fix | Delete
* Helper function to get scalar value right out of query and optional arguments
[194] Fix | Delete
*
[195] Fix | Delete
* Examples:
[196] Fix | Delete
* $name = $db->getOne("SELECT name FROM table WHERE id=1");
[197] Fix | Delete
* $name = $db->getOne("SELECT name FROM table WHERE id=?i", $id);
[198] Fix | Delete
*
[199] Fix | Delete
* @param string $query - an SQL query with placeholders
[200] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[201] Fix | Delete
* @return string|FALSE either first column of the first row of resultset or FALSE if none found
[202] Fix | Delete
*/
[203] Fix | Delete
public function getOne()
[204] Fix | Delete
{
[205] Fix | Delete
$query = $this->prepareQuery(func_get_args());
[206] Fix | Delete
if ($res = $this->rawQuery($query))
[207] Fix | Delete
{
[208] Fix | Delete
$row = $this->fetch($res);
[209] Fix | Delete
if (is_array($row)) {
[210] Fix | Delete
return reset($row);
[211] Fix | Delete
}
[212] Fix | Delete
$this->free($res);
[213] Fix | Delete
}
[214] Fix | Delete
return FALSE;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Helper function to get single row right out of query and optional arguments
[219] Fix | Delete
*
[220] Fix | Delete
* Examples:
[221] Fix | Delete
* $data = $db->getRow("SELECT * FROM table WHERE id=1");
[222] Fix | Delete
* $data = $db->getRow("SELECT * FROM table WHERE id=?i", $id);
[223] Fix | Delete
*
[224] Fix | Delete
* @param string $query - an SQL query with placeholders
[225] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[226] Fix | Delete
* @return array|FALSE either associative array contains first row of resultset or FALSE if none found
[227] Fix | Delete
*/
[228] Fix | Delete
public function getRow()
[229] Fix | Delete
{
[230] Fix | Delete
$query = $this->prepareQuery(func_get_args());
[231] Fix | Delete
if ($res = $this->rawQuery($query)) {
[232] Fix | Delete
$ret = $this->fetch($res);
[233] Fix | Delete
$this->free($res);
[234] Fix | Delete
return $ret;
[235] Fix | Delete
}
[236] Fix | Delete
return FALSE;
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
/**
[240] Fix | Delete
* Helper function to get single column right out of query and optional arguments
[241] Fix | Delete
*
[242] Fix | Delete
* Examples:
[243] Fix | Delete
* $ids = $db->getCol("SELECT id FROM table WHERE cat=1");
[244] Fix | Delete
* $ids = $db->getCol("SELECT id FROM tags WHERE tagname = ?s", $tag);
[245] Fix | Delete
*
[246] Fix | Delete
* @param string $query - an SQL query with placeholders
[247] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[248] Fix | Delete
* @return array enumerated array of first fields of all rows of resultset or empty array if none found
[249] Fix | Delete
*/
[250] Fix | Delete
public function getCol()
[251] Fix | Delete
{
[252] Fix | Delete
$ret = array();
[253] Fix | Delete
$query = $this->prepareQuery(func_get_args());
[254] Fix | Delete
if ( $res = $this->rawQuery($query) )
[255] Fix | Delete
{
[256] Fix | Delete
while($row = $this->fetch($res))
[257] Fix | Delete
{
[258] Fix | Delete
$ret[] = reset($row);
[259] Fix | Delete
}
[260] Fix | Delete
$this->free($res);
[261] Fix | Delete
}
[262] Fix | Delete
return $ret;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
/**
[266] Fix | Delete
* Helper function to get all the rows of resultset right out of query and optional arguments
[267] Fix | Delete
*
[268] Fix | Delete
* Examples:
[269] Fix | Delete
* $data = $db->getAll("SELECT * FROM table");
[270] Fix | Delete
* $data = $db->getAll("SELECT * FROM table LIMIT ?i,?i", $start, $rows);
[271] Fix | Delete
*
[272] Fix | Delete
* @param string $query - an SQL query with placeholders
[273] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[274] Fix | Delete
* @return array enumerated 2d array contains the resultset. Empty if no rows found.
[275] Fix | Delete
*/
[276] Fix | Delete
public function getAll()
[277] Fix | Delete
{
[278] Fix | Delete
$ret = array();
[279] Fix | Delete
$query = $this->prepareQuery(func_get_args());
[280] Fix | Delete
if ( $res = $this->rawQuery($query) )
[281] Fix | Delete
{
[282] Fix | Delete
while($row = $this->fetch($res))
[283] Fix | Delete
{
[284] Fix | Delete
$ret[] = $row;
[285] Fix | Delete
}
[286] Fix | Delete
$this->free($res);
[287] Fix | Delete
}
[288] Fix | Delete
return $ret;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/**
[292] Fix | Delete
* Helper function to get all the rows of resultset into indexed array right out of query and optional arguments
[293] Fix | Delete
*
[294] Fix | Delete
* Examples:
[295] Fix | Delete
* $data = $db->getInd("id", "SELECT * FROM table");
[296] Fix | Delete
* $data = $db->getInd("id", "SELECT * FROM table LIMIT ?i,?i", $start, $rows);
[297] Fix | Delete
*
[298] Fix | Delete
* @param string $index - name of the field which value is used to index resulting array
[299] Fix | Delete
* @param string $query - an SQL query with placeholders
[300] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[301] Fix | Delete
* @return array - associative 2d array contains the resultset. Empty if no rows found.
[302] Fix | Delete
*/
[303] Fix | Delete
public function getInd()
[304] Fix | Delete
{
[305] Fix | Delete
$args = func_get_args();
[306] Fix | Delete
$index = array_shift($args);
[307] Fix | Delete
$query = $this->prepareQuery($args);
[308] Fix | Delete
[309] Fix | Delete
$ret = array();
[310] Fix | Delete
if ( $res = $this->rawQuery($query) )
[311] Fix | Delete
{
[312] Fix | Delete
while($row = $this->fetch($res))
[313] Fix | Delete
{
[314] Fix | Delete
$ret[$row[$index]] = $row;
[315] Fix | Delete
}
[316] Fix | Delete
$this->free($res);
[317] Fix | Delete
}
[318] Fix | Delete
return $ret;
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* Helper function to get a dictionary-style array right out of query and optional arguments
[323] Fix | Delete
*
[324] Fix | Delete
* Examples:
[325] Fix | Delete
* $data = $db->getIndCol("name", "SELECT name, id FROM cities");
[326] Fix | Delete
*
[327] Fix | Delete
* @param string $index - name of the field which value is used to index resulting array
[328] Fix | Delete
* @param string $query - an SQL query with placeholders
[329] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the query
[330] Fix | Delete
* @return array - associative array contains key=value pairs out of resultset. Empty if no rows found.
[331] Fix | Delete
*/
[332] Fix | Delete
public function getIndCol()
[333] Fix | Delete
{
[334] Fix | Delete
$args = func_get_args();
[335] Fix | Delete
$index = array_shift($args);
[336] Fix | Delete
$query = $this->prepareQuery($args);
[337] Fix | Delete
[338] Fix | Delete
$ret = array();
[339] Fix | Delete
if ( $res = $this->rawQuery($query) )
[340] Fix | Delete
{
[341] Fix | Delete
while($row = $this->fetch($res))
[342] Fix | Delete
{
[343] Fix | Delete
$key = $row[$index];
[344] Fix | Delete
unset($row[$index]);
[345] Fix | Delete
$ret[$key] = reset($row);
[346] Fix | Delete
}
[347] Fix | Delete
$this->free($res);
[348] Fix | Delete
}
[349] Fix | Delete
return $ret;
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Function to parse placeholders either in the full query or a query part
[354] Fix | Delete
* unlike native prepared statements, allows ANY query part to be parsed
[355] Fix | Delete
*
[356] Fix | Delete
* useful for debug
[357] Fix | Delete
* and EXTREMELY useful for conditional query building
[358] Fix | Delete
* like adding various query parts using loops, conditions, etc.
[359] Fix | Delete
* already parsed parts have to be added via ?p placeholder
[360] Fix | Delete
*
[361] Fix | Delete
* Examples:
[362] Fix | Delete
* $query = $db->parse("SELECT * FROM table WHERE foo=?s AND bar=?s", $foo, $bar);
[363] Fix | Delete
* echo $query;
[364] Fix | Delete
*
[365] Fix | Delete
* if ($foo) {
[366] Fix | Delete
* $qpart = $db->parse(" AND foo=?s", $foo);
[367] Fix | Delete
* }
[368] Fix | Delete
* $data = $db->getAll("SELECT * FROM table WHERE bar=?s ?p", $bar, $qpart);
[369] Fix | Delete
*
[370] Fix | Delete
* @param string $query - whatever expression contains placeholders
[371] Fix | Delete
* @param mixed $arg,... unlimited number of arguments to match placeholders in the expression
[372] Fix | Delete
* @return string - initial expression with placeholders substituted with data.
[373] Fix | Delete
*/
[374] Fix | Delete
public function parse()
[375] Fix | Delete
{
[376] Fix | Delete
return $this->prepareQuery(func_get_args());
[377] Fix | Delete
}
[378] Fix | Delete
[379] Fix | Delete
/**
[380] Fix | Delete
* function to implement whitelisting feature
[381] Fix | Delete
* sometimes we can't allow a non-validated user-supplied data to the query even through placeholder
[382] Fix | Delete
* especially if it comes down to SQL OPERATORS
[383] Fix | Delete
*
[384] Fix | Delete
* Example:
[385] Fix | Delete
*
[386] Fix | Delete
* $order = $db->whiteList($_GET['order'], array('name','price'));
[387] Fix | Delete
* $dir = $db->whiteList($_GET['dir'], array('ASC','DESC'));
[388] Fix | Delete
* if (!$order || !dir) {
[389] Fix | Delete
* throw new http404(); //non-expected values should cause 404 or similar response
[390] Fix | Delete
* }
[391] Fix | Delete
* $sql = "SELECT * FROM table ORDER BY ?p ?p LIMIT ?i,?i"
[392] Fix | Delete
* $data = $db->getArr($sql, $order, $dir, $start, $per_page);
[393] Fix | Delete
*
[394] Fix | Delete
* @param string $iinput - field name to test
[395] Fix | Delete
* @param array $allowed - an array with allowed variants
[396] Fix | Delete
* @param string $default - optional variable to set if no match found. Default to false.
[397] Fix | Delete
* @return string|FALSE - either sanitized value or FALSE
[398] Fix | Delete
*/
[399] Fix | Delete
public function whiteList($input,$allowed,$default=FALSE)
[400] Fix | Delete
{
[401] Fix | Delete
$found = array_search($input,$allowed);
[402] Fix | Delete
return ($found === FALSE) ? $default : $allowed[$found];
[403] Fix | Delete
}
[404] Fix | Delete
[405] Fix | Delete
/**
[406] Fix | Delete
* function to filter out arrays, for the whitelisting purposes
[407] Fix | Delete
* useful to pass entire superglobal to the INSERT or UPDATE query
[408] Fix | Delete
* OUGHT to be used for this purpose,
[409] Fix | Delete
* as there could be fields to which user should have no access to.
[410] Fix | Delete
*
[411] Fix | Delete
* Example:
[412] Fix | Delete
* $allowed = array('title','url','body','rating','term','type');
[413] Fix | Delete
* $data = $db->filterArray($_POST,$allowed);
[414] Fix | Delete
* $sql = "INSERT INTO ?n SET ?u";
[415] Fix | Delete
* $db->query($sql,$table,$data);
[416] Fix | Delete
*
[417] Fix | Delete
* @param array $input - source array
[418] Fix | Delete
* @param array $allowed - an array with allowed field names
[419] Fix | Delete
* @return array filtered out source array
[420] Fix | Delete
*/
[421] Fix | Delete
public function filterArray($input,$allowed)
[422] Fix | Delete
{
[423] Fix | Delete
foreach(array_keys($input) as $key )
[424] Fix | Delete
{
[425] Fix | Delete
if ( !in_array($key,$allowed) )
[426] Fix | Delete
{
[427] Fix | Delete
unset($input[$key]);
[428] Fix | Delete
}
[429] Fix | Delete
}
[430] Fix | Delete
return $input;
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
/**
[434] Fix | Delete
* Function to get last executed query.
[435] Fix | Delete
*
[436] Fix | Delete
* @return string|NULL either last executed query or NULL if were none
[437] Fix | Delete
*/
[438] Fix | Delete
public function lastQuery()
[439] Fix | Delete
{
[440] Fix | Delete
$last = end($this->stats);
[441] Fix | Delete
return $last['query'];
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
/**
[445] Fix | Delete
* Function to get all query statistics.
[446] Fix | Delete
*
[447] Fix | Delete
* @return array contains all executed queries with timings and errors
[448] Fix | Delete
*/
[449] Fix | Delete
public function getStats()
[450] Fix | Delete
{
[451] Fix | Delete
return $this->stats;
[452] Fix | Delete
}
[453] Fix | Delete
[454] Fix | Delete
/**
[455] Fix | Delete
* protected function which actually runs a query against Mysql server.
[456] Fix | Delete
* also logs some stats like profiling info and error message
[457] Fix | Delete
*
[458] Fix | Delete
* @param string $query - a regular SQL query
[459] Fix | Delete
* @return mysqli result resource or FALSE on error
[460] Fix | Delete
*/
[461] Fix | Delete
protected function rawQuery($query)
[462] Fix | Delete
{
[463] Fix | Delete
$start = microtime(TRUE);
[464] Fix | Delete
$res = mysqli_query($this->conn, $query);
[465] Fix | Delete
$timer = microtime(TRUE) - $start;
[466] Fix | Delete
[467] Fix | Delete
$this->stats[] = array(
[468] Fix | Delete
'query' => $query,
[469] Fix | Delete
'start' => $start,
[470] Fix | Delete
'timer' => $timer,
[471] Fix | Delete
);
[472] Fix | Delete
if (!$res)
[473] Fix | Delete
{
[474] Fix | Delete
$error = mysqli_error($this->conn);
[475] Fix | Delete
[476] Fix | Delete
end($this->stats);
[477] Fix | Delete
$key = key($this->stats);
[478] Fix | Delete
$this->stats[$key]['error'] = $error;
[479] Fix | Delete
$this->cutStats();
[480] Fix | Delete
[481] Fix | Delete
$this->error("$error. Full query: [$query]");
[482] Fix | Delete
}
[483] Fix | Delete
$this->cutStats();
[484] Fix | Delete
return $res;
[485] Fix | Delete
}
[486] Fix | Delete
[487] Fix | Delete
protected function prepareQuery($args)
[488] Fix | Delete
{
[489] Fix | Delete
$query = '';
[490] Fix | Delete
$raw = array_shift($args);
[491] Fix | Delete
$array = preg_split('~(\?[nsiuap])~u',$raw,null,PREG_SPLIT_DELIM_CAPTURE);
[492] Fix | Delete
$anum = count($args);
[493] Fix | Delete
$pnum = floor(count($array) / 2);
[494] Fix | Delete
if ( $pnum != $anum )
[495] Fix | Delete
{
[496] Fix | Delete
$this->error("Number of args ($anum) doesn't match number of placeholders ($pnum) in [$raw]");
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function