SELECT where row value contains string MySQL - php

How can I select a row in my MySQL DB where the value of a column contains 'XcodeDev' for example?
I tried:
SELECT * FROM Accounts WHERE Username LIKE '$query'
But it only selects a row were the Username value is exactly the same to the query.
What can I do to achieve what I want?

Use the % wildcard, which matches any number of characters.
SELECT * FROM Accounts WHERE Username LIKE '%query%'

This should work:
SELECT * FROM Accounts WHERE Username LIKE '%$query%'

My suggestion would be
$value = $_POST["myfield"];
$Query = Database::Prepare("SELECT * FROM TABLE WHERE MYFIELD LIKE ?");
$Query->Execute(array("%".$value."%"));

SELECT * FROM Accounts WHERE Username LIKE '%$query%'
but it's not suggested. use PDO

Related

Mysql Search for a word/sentence that has extra characteres at the end PHP

Currently I have a query that searchs for sentences/words, it works almost as expected,
I have a regex expresion that searches for names in a table, expample:
function getNames($str){
$stmt = "SELECT * FROM users WHERE name = :name
OR name REGEXP :reg1
OR name REGEXP :reg2
OR name LIKE :lik1";
$query = self::$connection->prepare($stmt);
$query->execute(array(":name"=>$str,
":reg1"=>"^$str" . "[a-zA-Z\s]*$",
":reg2"=>"^[a-zA-Z]*[\s]*[$str][a-zA-Z]"
":lik1"=>"%" . $str . "%"
));
return $query->fetchAll(PDO::FETCH_ASSOC);
}
Let's suppose my table contains the following values
Bob
Peter
Mark
David
John
If I run my query with Bob as $name value it gets it but I would like to be able to find Bob when I run the query using BobsomeLettersExtra or Bob something as $name value
Is there a way to do this using REGEXP or LIKE ?
SELECT * FROM users WHERE name LIKE '%".$name."%'
above query should be enough to get the result. You should validate data before you enter data to the table if not please use the regex as well
"SELECT * FROM users WHERE name LIKE '%".$name."%' AND REGEXP ^".$name."[a-zA-Z]*$"
UPDATE
sorry if i have misunderstand the question please try this
"Select * from users WHERE '".$name."' LIKE CONCAT(name , '%')"
You may try below Query with where Clause for LIKE :
"SELECT * FROM users WHERE name = ".$name." OR name LIKE '%".$name."%' OR name REGEXP ^".$name."[a-zA-Z]*$"

Why Mysql is returning wrong count of rows?

I have in database two information that matches with the names and the address. Instead of returning 2, this code is returning 21. Please see below;
(Table)-employees
id
name
address
$select = mysql_query("
SELECT *
FROM employees
WHERE name LIKE '%John%'
OR name LIKE '%Johanson%'
AND address='Streetford End'
");
$count = mysql_num_rows($select);
echo $count
This might be helpful:
SELECT * FROM employees WHERE (name LIKE '%John%' OR name LIKE '%Johanson%')
AND address='Streetford End'

how to check that a field in mysql contains a specific word

I need to check if a field in mysql contains a specific word using select query
exmaple: field 'name' = test1,test2,test3
Select * from table where name Like '%test3%
it returns empty, any help
use find_in_set():
SELECT * from `table` where FIND_IN_SET(name, 'test1,test2,test3 ');
use this to find word like test1,test2 etc
Select * from table where name Like '%test%'
Try this:-
SELECT * FROM table WHERE name IN ( 'test1',' test2', 'test3');
Add single quotes after '%test3%:
SELECT * FROM table WHERE name LIKE '%test3%

How can I select rows only where first digit is a number from 0 to 9?

As far as I know I can do something like:
"SELECT *
FROM my_table
WHERE my_field LIKE '0%'
OR my_field LIKE '1%'
OR my_field LIKE '2%' ";
Is there a way to achieve this with a regular expression or something like this:
"SELECT *
FROM my_table
WHERE my_field LIKE [only first char is 0-9]"??
EDIT: The field is not numeric and it can be something like "1 People", "211 Pies" and so on.
Try this
SELECT *
FROM BadlyDesignedTable
WHERE AnswerColumn RLIKE '^[0-9]+'
I was wondering if it was even possible to regex in where, found it on google in 30 seconds.
SELECT * FROM table WHERE field REGEXP '^[0-9]'
Select * From my_table Where (REGEXP_LIKE(my_field, '[[:digit:]]%'))
The (REGEXP_LIKE(Source_String, '[[:character class:]]')) is a function you can use for numerous issues such as the one you have. Simply use it to tell it to do that for the first digit.
http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm
EDIT: Select * From my_table Where (SUBSTR(my_field,1,1) = '[[:digit:]]')
Tried this in a similar query of mine, and it works.
SELECT *
FROM my_table
WHERE left(my_field,1) REGEXP '^[0-9]+';
If you have MSSQL you can use
SELECT *
FROM my_table
WHERE isNumeric(left(my_field,1)) = 1
A simple one without regular expressions:
SELECT *, SUBSTR(my_field, 1, 1) AS mf FROM my_table HAVING mf BETWEEN '0' AND '9';
The quotes in the between clause are important!

php mysql - search like - variable

I have a form where a user can type in the firstname to search, my query is not returning the correct results, what am I doing wrong?
$sfn = $_POST["Text1"];
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '$sfn'";
...
Maybe you should add %-signs to your keyword like this:
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'";
Your query will only return rows where firstname is equal to $_POST["Text1"]. When you use LIKE you can use a wildcard (%) to represent any number of characters.
This will find rows where firstname starts with $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '$sfn%'
This will find rows where firstname ends with $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn'
This will find rows where firstname contains $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'
Note: Never use variables from $_POST without escaping them first. What if I searched for "O'Neil" (or worse "'; DROP TABLE ex_users; -- ")?
You should use %searchterm% - include the % wildcards.
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'";
It should be
"SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'"

Categories