Radicore Forum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » RADICORE development » Bug Reports » getCount function throws error when sql statement starts with whitespace
getCount function throws error when sql statement starts with whitespace [message #5864] Wed, 07 September 2016 12:00 Go to next message
kong is currently offline  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

Re: getCount function throws error when sql statement starts with whitespace [message #5866 is a reply to message #5864] Thu, 08 September 2016 03:34 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2347
Registered: April 2006
Location: Surrey, UK
Senior Member
I have added your fix to the getCount() method within std.table.class.inc as follows:
    // ****************************************************************************
    function getCount ($where=null)
    // get count of records that satisfy selection criteria in $where.
    {
        if (strlen(trim(preg_replace("/^\s+/u", "", $where))) > 0) {
            $count = $this->_dml_getCount($where);
            return $count;
        } else {
            return 0;
        } // if

    } // getCount


Re: getCount function throws error when sql statement starts with whitespace [message #5873 is a reply to message #5866] Fri, 09 September 2016 21:27 Go to previous messageGo to next message
kong is currently offline  kong
Messages: 90
Registered: December 2011
Member
That won't work, as it does not make changes to the $where variable.
Recommend this:

    function getCount ($where=null)
    // get count of records that satisfy selection criteria in $where.
    {
        $where = trim($where); 
        if (strlen($where) > 0) {
            $count = $this->_dml_getCount($where);
            return $count;
        } else {
            return 0;
        } // if

    } // getCount

[Updated on: Fri, 09 September 2016 21:29]

Report message to a moderator

Re: getCount function throws error when sql statement starts with whitespace [message #5875 is a reply to message #5873] Sat, 10 September 2016 05:36 Go to previous message
AJM is currently offline  AJM
Messages: 2347
Registered: April 2006
Location: Surrey, UK
Senior Member
Whoops! Silly me! I shall put this fix in the next release.

Previous Topic: Radicore v1.95 - Postgresal 9.5.3 - System crash after pressing submit button
Next Topic: UPDATE1 scrolling skips validation after SubmitNext
Goto Forum:
  


Current Time: Thu Mar 28 16:56:19 EDT 2024

Total time taken to generate the page: 0.02950 seconds