Case insensitive username checking? [duplicate] - php

I'm trying to select some data from a mysql table but I cannot get the Where comparison to be case insensitive, I tried using LOWER:
$wildcard = $_GET['q'];
$query = "SELECT id, name, departamento FROM gestionDoc_cargos WHERE (LOWER(name) LIKE '%' LOWER(:wildcard) '%' OR LOWER(departamento) LIKE '%' LOWER(:wildcard) '%')";
try{
$result = DB::getInstance()->prepare($query);
$result->bindParam(':wildcard', $wildcard, PDO::PARAM_STR);
$result->execute();
$result = $result->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
die($e->getMessage());
}
print_r($result);
echo json_encode(array_values($result));
but I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOWER('C') '%' OR LOWER(departamento) LIKE '%' LOWER('C') '%')' at line 1
and if I remove the LOWER from the query I get a case sensitive select.

This
...snip... ) LIKE '%' LOWER(:wildcard) '%' OR ...snip
is incorrect. You've got a string ('%') followed by a function call (LOWER()) followed by another string, and they're just sitting there - no connecting logic, no concatenation, blah blah blah .
It should be
... LIKE CONCAT('%', LOWER(:wildcard), '%') OR ...
And by default, mysql comparisons ARE case insensitive, unless you force a binary comparison, or you're using a case sensitive collation on your db/table.

Related

Why this PHP PDO Mysql code not working?

I used Like operator and pass all the parameter But Still the following code is not working:
public function get_locations($lang, $suggest){
$this->lang = $lang;
$this->suggest = $suggest;
$sql = "SELECT l.location_id, l.location_name_col
FROM test_db.location_translations as l
WHERE l.location_name_col like LIKE :suggest
AND l.language_code = :lang
";
$params = array(':suggest'=>"%".$this->suggest."%", ':lang'=> $this->lang);
$stmt = $this->conn->prepare($sql);
$stmt->execute($params);
}
I am getting the following erros:
PDOException' with message 'SQLSTATE[42000]: Syntax error or access
violation: 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 'LIKE '%A%'
AND l.language_code = 'en'' at line 3
please help me.
Well looking at your error code the problem has to do with the 'LIKE' parameter, I see that you are using 'like' and 'LIKE'. I think it should look like this:
$sql = "SELECT l.location_id, l.location_name_col FROM
test_db.location_translations as l WHERE l.location_name_col LIKE
:suggest AND l.language_code = :lang ";
What if you run it again with the above code, what happens then?

Laravel Concat - Syntax or Access Violation

Trying to write an MySQL Query to search on users based on two columns :
first_name
surname
I'd like to Concatenate first_name and surname as name and then write a search query based on that.
I've currently written some PHP code, But I keep getting the error :
SQLSTATE[42000]: Syntax error or access violation: 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 '%?%' at line
1 (SQL: select users.* from users where CONCAT(first_name, ' ',
surname) LIKE %Stu%)
My current PHP code is as follows :
// Search Based on Q..
$q = $request->input('q');
$results = User::select('users.*')->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE %?%", [$q])->get();
I suspect I'm just getting the syntax wrong, But any help would be appreciated.
Thank You!
Move % to parameter:
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE ?", ['%' . $q . '%'])
->get();
two errors in your query first as fullname not mention and second like operator.. i have fix these errors
$q = $request->input('q');
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) as fullname
LIKE ?", ['%' . $q . '%'])->get();

Error when preparing query - MySQLIi class "SHOW TABLES LIKE" error

I am using this database class for my project: GitHub.
When trying to execute a SHOW query to determine whether a table exists or not I receive this error:
Fatal error: Problem preparing query (SHOW TABLES LIKE users) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'users' at line 1 in mysqli.php on line 679
The query looks like this:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE " . $TABLE);
$TABLE is obviously filled with a string, I double checked that.
Any idea what could be wrong?
You probably missed the quotes:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE '" . $TABLE . "'");
The like statement it's value is wrong.
You should use:
BAD
$result = $DATABASE->rawQuery("SHOW TABLES LIKE 'value here' ");
Good
$result = $DATABASE->rawQuery("SHOW TABLES LIKE ? ");
$DATABASE->addParam($table);
I think you allso want to add % in front and after your $table :)

PDO syntax error for SELECT query

I have a basic query which gets all fields and exports the file but it keeps giving me an error. My code looks like this:
$array = ['users'];
foreach($array AS $i){
$file = $i.'.sql';
$stmt = $pdo->prepare("SELECT * FROM ? INTO OUTFILE ?");
try {
$stmt->execute(array($i,$file));
} catch (PDOException $e) {
$log .= $e -> getMessage().'........ \n ';
}
}
I keep getting this error how ever:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' INTO OUTFILE 'users.sql'' at line 1.
What is the correct syntax for this ?
It(Syntax) should be the other way around
SELECT * INTO OUTFILE ? FROM ?;
See the documentation here
EDIT :
As JAL clarified, The table name cannot be passed as a parameter under a PreparedStatement. So your query should be like
SELECT * INTO OUTFILE ? FROM users;
You cannot prepare a statement where the table name is a parameter.
See Can PHP PDO Statements accept the table or column name as parameter?

Mysql trouble with like and or like

SELECT `id`, `name_person`, `person_content`, `datetime`
FROM (`achievers_unverified`)
WHERE ` name_person LIKE '%ved%'
OR ` person_content LIKE '%ved%' LIMIT 10
This is the sql query i am trying to use where ved is the search term.
i am gettin a 1064 error.
the codeigniter code generating it is.
$this->db->select($select)
->from($table)
->like($str[1], $query, 'both')
->or_like($str[2], $query, 'both')
->limit($offset+10, $offset);
this is the error :
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'person_content LIKE '%ved%' LIMIT 10' at line 3.
$str = explode(",", $select);
where $select = id, name_person, person_content, datetime
found the solution use trim($str[0]) and trim($str[1]) the sapce was creating the problem.

Categories