MySQL query to get user where email equals some variable - php

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

Related

Where equals SQL ignoring whitespaces

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"

How to get result from mysql that contains in variable?

I'm not getting the result I need and I'm sure it is a small problem here.
I have a column(mfg_req) in my database which have a record with 10M.
My variable in php does have the text 10M40SABCDE.
What I want is to search in my table and get the results starting with this variable.
My MYSQL query does look like below but no results:
SELECT * FROM specific_req WHERE mfg_req LIKE '10M40SABCDE%'
I also tried the below query but no results
SELECT * FROM specific_req WHERE mfg_req LIKE '%10M40SABCDE%'
Also tried the below but it shows me all records except the one I need with 10M
SELECT * FROM specific_req WHERE '10M40SABCDE' LIKE CONCAT('%',mfg_req)
I have tried to put the % behind mfg_req but then it will show me all records including the one I need.
I cannot figure it out how to get the result I need. If someone can help me with my query I would appreciate it a lot.
Thanks!
Let's say:
$var = '10M40SABCDE';
...then your SQL statement must be:
$sql = "SELECT * FROM specific_req WHERE mfg_req LIKE '{$var}%'";
It would be best if you show a snippet of your code, too.

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

PHP inbox System

I have a problem with creating an inbox System.
What i'm trying to do is in "ViewMessages.php" I am trying to pull information from a MYSQL Table to show messages.
My First Statement is:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName'");
But I realised a flaw, it will only show messages sent 1 way. I Tried something like:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName' OR FromUName='$ToUName' AND ToUName='$FromUName'");
This failed. Can anyone shed some light to show both messages from both parties?
How about union?
$MessageQuery = mysql_query("(SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName') UNION (SELECT * FROM messages WHERE FromUName='$ToUName' AND ToUName='$FromUName')");
Note: If you need messages in any particular order, you can use ORDER BY at the end of the query, hoping you have something like message_id or timestamp attached to each.
SELECT *
FROM messages
WHERE '$ToUName' in (ToUName, FromUName)
OR '$FromUName' in (ToUName, FromUName)
or if you prefer columns listed first in your query
SELECT *
FROM messages
WHERE ToUName in ('$ToUName', '$FromUName')
OR FromUName in ('$ToUName', '$FromUName')
You have boolean operators mixed up, try put some () into that.. (a AND b) OR (c AND d).
Also what you'd acomplish is getting all messages between you and the one other contact. Are you aware of that?
Try This:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' || FromUName='$FromUName'");

Categories