Where equals SQL ignoring whitespaces - php

When I search for results from my table with the use of WHERE equals Clause I've got results that I would prefer to be different. For instance, when I look for result and my query is
SELECT * FROM users WHERE login="lapis" result is the same as it would be in
SELECT * FROM users WHERE login="lapis ". The whitespace at the end is ignored which is problem in my code as I use this query for my php login script and anyone can log into account with proper password but technically without proper login (it should be "lapis", not "lapis "). Is there anything I can do with it?

Your question isn't very detailed, but from what I can gather a simple preg_replace() will do what you want.
For example,
$login = preg_replace('/\s+/', '', 'lapis '); // This will output: lapis
$conn->query("SELECT * FROM users WHERE login='".$login."'")
preg_replace with the characters I included will remove ALL whitespace, including tabs and other forms.

You can do your query like the following.
SELECT * FROM users WHERE login LIKE "your_input_here"
Using LIKE solves the problem.
For example:
SELECT * FROM users WHERE login LIKE "lapis"
Only will get values that 'lapis' as login. And
SELECT * FROM users WHERE login LIKE "lapis "
Only will get values that 'lapis ' as login.

This removes whitespace from the end in your result.
SELECT RTRIM(username) FROM users WHERE login="lapis"

Related

MySQL LIKE query always returns 0

I'm Trying to search for usernames using LIKE query, however the query always return 0 and the usernames does exists! I'm using MYSQL terminal that with MAMP PRO server and here what i tried:
SELECT * FROM userlogin WHERE username LIKE '٪w٪';
SELECT * FROM userlogin WHERE username LIKE '٪w';
SELECT * FROM userlogin WHERE username LIKE 'w٪';
SELECT * FROM userlogin WHERE username LIKE '٪?٪';
Always the result is the same 0 what can be the problem!?
You are using percent character from Arabic encoding, copy following character and all will be fine (or switch your keyboard on English based keyboard and then type):
%
I doubt that it returns 0 - I suspect you mean the queries return no rows. Also you did not provide any details of the records you expected the query to return.
Applying further guesswork, I think the first 2 queries are intended to return records where the username starts with zW - in which case you should be specify a wildcard in your literal, e.g.
SELECT * FROM userlogin WHERE username LIKE '٪w%';
From the manual:
With LIKE you can use the following two wildcard characters in the pattern:
% matches any number of characters, even zero characters.
_ matches exactly one character.
Hi Walid Naceri You have wrong special character use %w% Like this

search without hyphen in mysql

Mysql database contains below kind of values :
'AE01-1056 Ricoh Aficio' OR 'Ricoh AE01-1087 (AE01-1069)' etc
AS if am a normal user i will search the product name in simple text like
AE011056 ... but the result is not found.
i hav tried this query:
$q="SELECT * FROM mytable WHERE (p.product_name LIKE '$name%' OR c.category_name LIKE '$name%' OR pm.name LIKE '$name%')";
What change should i make in my query to get the product , because i have tried LIKE operator & it's not working for me.
Use replace function
$q="SELECT * FROM mytable
WHERE (REPLACE(p.product_name,'-','') LIKE '%$name%'
OR REPLACE(c.category_name,'-','') LIKE '%$name%'
OR REPLACE(pm.name ,'-','') LIKE '%$name%')";
I think there are only two ways:
1. Manipulate search string
If you knwo, users are often search for a code and don't use nessesary hyphens, check the searchstring bevor searching if it follows a given format an insert the hypen if it is missing.
2. replace all hyphens in the where-statement
see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
Depending on your setup, solution one might be the more performant solution, since you only have to do one instead multiple stringmanipulations.

How to use LIKE operator against multiple values

I have this query :
select * from users where mailaddress
NOT like '%banned_domain1.com%'
AND mailaddress NOT like '%banned_domain2.com%'
AND mailaddress NOT like '%banned_domain3.com%' ;
I want to make it more simple , I executed this query from command line :
select * from users where mailaddress
NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;
I got MySQL error :
ERROR 1241 (21000): Operand should contain 1 column(s)
You can use NOT REGEXP
SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';
See live demo
Instead of "Like" use "In" and format the email address like this:
select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '#') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');
The SUBSTR will remove the # and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN.
Cheers!
you have to mention the column every time
select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'
however as Bruno Domingues said use NOT IN that will be more easy
You cannot simplify your query. You need one LIKE per condition.

MySQL query to get user where email equals some variable

First of all i don't know where to start doing this. That's why i am posting this question. Maybe you guys go for down-vote or close or flag it.
Here is the problem.
<?php
$gmail = "dipesh#some.com"
$split = explode('#',$service['User']['email']);
?>
The code above outputs an array with two elements inside it: one for dipesh and other for some.com. That is correct as expected.
Now I want a MySQL query that will perform select query to get record for dipesh#.
Example
SELECT * FROM users WHERE users.email LIKE 'dipesh';
The code above returns all the records that contain dipesh, but I want only the ones that contain dipesh#.
Does SELECT * FROM users WHERE users.email LIKE '%dipesh'; work?
If it does then what is the difference between the two?
Thanks
Your PHP code seems unrelated to your problem, but it sounds like you want email = 'dipesh#', which matches dipesh# literally. If you want dipesh# followed by anything, use email LIKE 'dipesh#%'. The % is a wildcard meaning "match zero or more of any character."
try this
SELECT * FROM users WHERE users.email LIKE 'dipesh#';
and if you want to dipesh# followed by anything else
use this
SELECT * FROM users WHERE users.email LIKE 'dipesh#%';
You probably want to try this
"SELECT * FROM users WHERE users.email LIKE 'dipesh#%'";
Hope this helps
SELECT * FROM users WHERE email LIKE 'dipesh#%';

MySQL '=' operator not returning result

I have a database where I have an email field, among others.
When I do a SELECT statement like :
SELECT * FROM table_name WHERE email = 'abcd#gmail.com';
...it returns an empty set, even though I can see the entry in the table.
The same statement works when I use:
SELECT * FROM table_name WHERE fname = 'abcd';
What could be going wrong?
use trim in your query
SELECT * FROM table_name WHERE TRIM(`email`) = 'abcd#gmail.com';
http://sqlfiddle.com/#!2/90a95/3
Maybe there are some spaces in the entry that is stored in MySQL, Check if the stored email is exactly that without any blank space or unexpected character. You can also try the following to confirm this
SELECT * FROM table_name WHERE email LIKE '%abcd#gmail.com%'
Are you sure you do not have additional spaces? try with LIKE clause
SELECT * FROM table_name WHERE email LIKE '%abcd#gmail.com%'
Does the entry contain extra spaces (tabs etc.) maybe?
You can try to:
SELECT * FROM table_name WHERE email LIKE 'abcd#gmail.com%'
and
SELECT * FROM table_name WHERE email LIKE '%abcd#gmail.com'
to see if that's the case
use trim in the query .It will not consider spaces.
Try this and check whether you are getting results for abcd#gmail.com or not.
SELECT *
FROM table_name
WHERE email LIKE '%abcd#%'
I got it to work after a small but unintended change.
My old PHP query goes like this :
$res = mysql_query("SELECT * FROM books WHERE email = '$email'");
My new and changed query goes like this :
$res = mysql_query("SELECT * FROM books WHERE email = ' $email'");
For some reason, mysql_real_escape_string() was adding one extra space at the beginning, I think, which wasn't visible in PHPMyAdmin.
try below code. thinks it will help you.
SELECT * FROM table_name WHERE `email' = 'abcd#gmail.com';

Categories