Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 585 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a user variables in MySQL LIKE clause?

#1
I am trying to setup a few simple SQL scripts to help with some short term DB administration.
As such, I'm setting up variables to try to make it easier to reuse these scripts.

The problem I'm having is specifically with the LIKE clause.

SET @email = '[email protected]';

SELECT email from `user` WHERE email LIKE '%@email%';

So I want to have it finding results based on the email SET in the variable. The query works if I manually enter the email into the LIKE clause.

How can I get the LIKE clause to work with the user variable?

UPDATE:
@dems's answer works for this simple case, but I'm having trouble with a more complex query.

SET @email = '[email protected]';

SELECT project.projectno, project.projectname, login.username,
CONCAT(login.firstname, ' ', login.lastname), projectuser.title
FROM projectuser
INNER JOIN login ON projectuser.uid = login.uid
LEFT JOIN project ON projectuser.pid = project.pid
WHERE login.username LIKE CONCAT ('%', @email, '%')

Gives me the error "FUNCTION mydb.CONCAT does not exist"

The query works without the CONCAT():

SET @email = '[email protected]';

SELECT project.projectno, project.projectname, login.username,
CONCAT(login.firstname, ' ', login.lastname), projectuser.title
FROM projectuser
INNER JOIN login ON projectuser.uid = login.uid
LEFT JOIN project ON projectuser.pid = project.pid
WHERE login.username LIKE @email
Reply

#2
SET @email = '[email protected]';

SELECT email from `user` WHERE email LIKE CONCAT('%', @email, '%');

Reply

#3
Using same syntax as oracle seems to work:

`SELECT email from user WHERE email LIKE '%' || @email || '%';`
Reply

#4
You may have error

Error Code: 1267. Illegal mix of collations for operation 'like' 0.016 sec

In this case you need to specify the same collation as used for your table like that:

SET @email = '[email protected]' COLLATE utf8_unicode_ci;
Reply

#5
I resolved using the CONCAT function before using LIKE statement:

SET @email = '[email protected]';
set @email = CONCAT('%',@email,'%');
SELECT email from `user` WHERE email LIKE @email;

It works fine for me
Reply

#6
BEGIN
SET @emailid = CONCAT('%', '[email protected]' ,'%');
SET @t1 =CONCAT('SELECT * FROM user WHERE email LIKE ''', @emailid, '''');
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
Reply

#7
No need to CONCAT just use '%'+ Variable +'%':

```sql
SET @email = '[email protected]';
SELECT email from `user` WHERE email LIKE '%' + @email + '%';
```
Reply

#8
Jan '22 I was having an issue getting the LIKE command to work. I discovered through echoing the variable value and my SQL statement that the quote function puts tick marks around the variable. code to get business type:

isset($_POST['button']) ? $businesstype = get_post($pdo, 'button'):
$businesstype = get_post($pdo, 'businesstype');

function get_post($pdo, $var)
{
return htmlentities($pdo->quote($_POST[$var]));
}


echo of $businesstype: 'Barber'

echo of SQL: Select BusinessName, MemberNum, Street, City FROM member WHERE verified = TRUE AND businesstype LIKE 'Barber' Order by featurelevel desc, membershipdate desc,businessname

Since the single quotes were already around the search value, to use LIKE I had to get the % after the first tick mark, and before the last.
I used a combination of a string replace function and concat operators:
replacing the original single quotes with nothing, and concatting the '% and %' around the search value.

$businesstype ="'%" . str_ireplace("'","",$businesstype) . "%'";
.
Worked for me.I hope it helps someone. Let me know if I am undoing the SQL injection safeguard somehow.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through