Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use like in where? #838

Closed
jinmarcus opened this issue Nov 19, 2017 · 4 comments
Closed

Unable to use like in where? #838

jinmarcus opened this issue Nov 19, 2017 · 4 comments

Comments

@jinmarcus
Copy link

jinmarcus commented Nov 19, 2017

$where['username Like'] = '%xxx%';

$this->db->table('admin_user')
        ->where($where)
        ->get()
        ->getResult();

Unable to use like in where?
it is error

mysqli_sql_exception #1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= '%xxx%'' at line 4
@gustavojm
Copy link
Contributor

gustavojm commented Nov 19, 2017

@jinmarcus
Copy link
Author

I know the use of like()
in https://github.com/bcit-ci/CodeIgniter4/blob/develop/system/Database/BaseBuilder.php
in 2809 line

protected function getOperator($str)
	{
		static $_operators;

		if (empty($_operators))
		{
			$_les = ($this->db->likeEscapeStr !== '') ? '\s+' . preg_quote(trim(sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar)), '/') : '';
			$_operators = [
				'\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
				'\s*<>?\s*', // <, <>
				'\s*>\s*', // >
				'\s+IS NULL', // IS NULL
				'\s+IS NOT NULL', // IS NOT NULL
				'\s+EXISTS\s*\(.*\)', // EXISTS(sql)
				'\s+NOT EXISTS\s*\(.*\)', // NOT EXISTS(sql)
				'\s+BETWEEN\s+', // BETWEEN value AND value
				'\s+IN\s*\(.*\)', // IN(list)
				'\s+NOT IN\s*\(.*\)', // NOT IN (list)
				'\s+LIKE\s+\S.*(' . $_les . ')?', // LIKE 'expr'[ ESCAPE '%s']
				'\s+NOT LIKE\s+\S.*(' . $_les . ')?' // NOT LIKE 'expr'[ ESCAPE '%s']
			];
		}

		return preg_match('/' . implode('|', $_operators) . '/i', $str, $match) ? $match[0] : false;
	}
'\s+LIKE\s+\S.*(' . $_les . ')?', // LIKE 'expr'[ ESCAPE '%s']

How should I use it?

@natanfelles
Copy link
Contributor

@lonnieezell
Copy link
Member

@jinmarcus what you've shown here is an internal function not meant for outside use (see the protected keyword), so you can't use that directly. A like method is already provided, as has already been pointed out.

While there may be some hacky way to make what you're trying to do work, it's not intended for that, nor will it be optimized for that use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants