MySQL '=' operator not returning result - php

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';

Related

Find Numeric code string in a SELECT mysql statement with LIKE

I save the single quote in my utf8 mysql database with ' .
now I just want to select all rows that include a ' in the name column. So I do:
SELECT * WHERE name LIKE "%'%"
but this will give me an empty result. if I do
SELECT * WHERE name LIKE "%&#%"
for example its working great.
What I am doing wrong? I want to search for the complete string '.
SELECT * FROM testone WHERE val LIKE '&#39'
result
From Table
Here is a working example of your query:
https://www.db-fiddle.com/f/ci63XiAPbKLaz7bRksJ4gW/1
The problem may depend on something else, e.g. how the ' is stored in the database.
May be the case that the ' is inserted as is, so try this:
SELECT * FROM test WHERE name LIKE "%'%";
In the same db-fiddle you can try this.
You can perform the search for both cases in the same query:
SELECT * FROM test WHERE (name LIKE "%'%" OR name LIKE "%'%");

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

How to select rows by substring? Similar to Google Suggest [duplicate]

I have mysql table that has a column that stores xml as a string. I need to find all tuples where the xml column contains a given string of 6 characters. Nothing else matters--all I need to know is if this 6 character string is there or not.
So it probably doesn't matter that the text is formatted as xml.
Question: how can I search within mysql?
ie
SELECT * FROM items WHERE items.xml [contains the text '123456']
Is there a way I can use the LIKE operator to do this?
You could probably use the LIKE clause to do some simple string matching:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
If you need more advanced functionality, take a look at MySQL's fulltext-search functions here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
Using like might take longer time so use full_text_search:
SELECT * FROM items WHERE MATCH(items.xml) AGAINST ('your_search_word')
SELECT * FROM items WHERE `items.xml` LIKE '%123456%'
The % operator in LIKE means "anything can be here".
Why not use LIKE?
SELECT * FROM items WHERE items.xml LIKE '%123456%'
you mean:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
When you are using the wordpress prepare line, the above solutions do not work. This is the solution I used:
$Table_Name = $wpdb->prefix.'tablename';
$SearchField = '%'. $YourVariable . '%';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name WHERE ColumnName LIKE %s", $SearchField) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);

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#%';

Categories