getCount function throws error when sql statement starts with whitespace [message #5864] |
Wed, 07 September 2016 12:00 |
kong
Messages: 90 Registered: December 2011
|
Member |
|
|
Use the getCount function as described here
http:// www.tonymarston.net/php-mysql/functions-and-variables.html#n otes.getcount
Plug in a $where string that starts out with some whitespace or tabs (because of certain formatting in sourcecode for example), followed with a SELECT statement, the result is an obscure error like this:
Quote:Fatal Error: Cannot extract token from: '*' (# 256)
This is caused by similar code across the different DML database classes, such as dml.mysqli.clacc.inc:
function getCount ($dbname, $tablename, $where)
// get count of records that satisfy selection criteria in $where.
{
$this->errors = array();
// connect to database
$this->connect($dbname) or trigger_error($this, E_USER_ERROR);
if (preg_match('/^(select )/ims', $where)) {
// $where starts with 'SELECT' so use it as a complete query
$this->query = $where;
} else {
// does not start with 'SELECT' so it must be a 'where' clause
if (empty($where)) {
$this->query = "SELECT SQL_CALC_FOUND_ROWS * FROM $tablename LIMIT 1";
} else {
$where = $this->adjustWhere($where);
$this->query = "SELECT SQL_CALC_FOUND_ROWS * FROM $tablename WHERE $where LIMIT 1";
} // if
} // if
The preg_match right after the connect to database checks for $where to start with select. When $where starts out with some whitespace it will assume that it is a where clause rather than select, and result in parsing error down the road.
This is easily fixed by adding for example adding this $where = trim(preg_replace("/^\s+/u", "", $where)); to the function getCount in std.table.class.inc
[Updated on: Wed, 07 September 2016 19:47] Report message to a moderator
|
|
|
|
|
|