I'm using MySQL version 5.0.51a and PHP to access the database, and this query returns nothing when it should return at least 2 rows which match the LIKE condition.
$result = mysql_query("SELECT * FROM user WHERE name LIKE '%".$search."%'OR email LIKE '%".$search."%' ORDER BY ".$order, $con);
The $search variable is 'Name',
there is no problem with ORDER, $order or $con, i've already tried that, and there are 2 rows where the name is 'Name', but somehow it can't find those rows and it returns nothing.
Does anybody know where the problem is?
Try removing double commas
$result = mysql_query("
SELECT
*
FROM user
WHERE name LIKE '%{$search}%' OR email LIKE '%{$search}%'
ORDER BY ".$order, $con);
It is mainly on the syntax as others have pointed out, here is another way:
$result = mysql_query(" SELECT * FROM user
WHERE '%{$search}%' IN (name, email )
ORDER BY ".$order.','. $con)
;
Please try this query:
$result = mysql_query("SELECT * FROM user WHERE name LIKE '%".$search."%'
OR email LIKE '%".$search."%'
ORDER BY ".$order.','.$con);
Related
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]*$"
Trying to write a funny query, don't know if it's possible.
$sql = "SELECT * FROM users WHERE Type = '1' ";
$sql .= "WHERE FirstName LIKE '%$searchq%' or LastName LIKE '%$searchq%'";
So selecting all from users from the column with type 1, and the concatenated part is whatever which has been entered in the search box. is this even possible? Or do I have to write up two different querys?
Help is much appreciated.
Just use parenthesis to organize your WHERE statements:
$sql = "SELECT * FROM users WHERE Type = '1'
AND (FirstName LIKE '%$searchq%' OR LastName LIKE '%$searchq%')";
I have some search functionality which allows a user to either select a name from a drop down list or type a persons name or part of it (wildcard functionality) into a search field.
The problem I am having is that if the drop down option is used then the search field won't be and vice versa.
I have tried some SQL as follows (please note the variables represent data sent in via a form):
SELECT id, name, age FROM player
WHERE player.id = '$id'
OR player.name LIKE '%$text%'
When the above is used the wildcard functionality works fine.
However if you select a player from the drop down then it returns all players. This is because the value for $text is nothing (empty) and LIKE '%%' means select everything and hence it selects all names.
I then tried the following:
SELECT id, name, age FROM player
WHERE player.id = '$id'
AND player.name LIKE '%$text%'
Now the drop down functionality works as expected but wild card searches do not work. This is because I assume that AND requires both statements to be true and because $id is nothing (empty) when just a wildcard entry is specified, the condition is never true.
Can anyone help me with some sql to ensure that both the dropdown and the wildcard search work in isolation of each other?
Thanks for your time and help in advance.
$params = ''
if($id !== ''){
$params = "player.id = '$id'";
} else if($text !== ''){
$params = "player.name LIKE '%$text%'";
}
$sql = "SELECT id,name,age FROM player WHERE {$params}";
mysql_query($sql); //if using mysql_
Require id conditionally:
$idCondition = "";
if ($id) {
$idCondition = "player.id = '$id' AND ";
}
$query = "SELECT id, name, age FROM player WHERE {$idCondition}player.name LIKE '%$text%'";
"SELECT id, name, age FROM player
WHERE (player.id = '$id' AND '$id' <> '')
OR (player.name LIKE '%$text%' AND '$text' <>'')"
I have several users that have items. I need each user to be able to search their items, and not see other people items's. This query still allows the executer to see every item in the dbase. Please help!
$findit is a variable earlier in the script that is what the user is looking for.
$username is set via a session cookie after they login.
$result = mysql_query( "SELECT * FROM prices WHERE
itemnum LIKE '%$findit%' ||
itemdesc LIKE '%$findit%' AND username = '$username' ORDER BY price");
You should be able to group the OR:
SELECT *
FROM prices
WHERE (itemnum LIKE '%$findit%'
OR itemdesc LIKE '%$findit%')
AND username = '$username'
ORDER BY price
Which will make the OR act as a single condition, and the username match as another, so that the username is required to be matched.
$result = mysql_query( "SELECT * FROM prices WHERE
(itemnum LIKE '%$findit%' OR itemdesc LIKE '%$findit%')
AND username = '$username' ORDER BY price");
notice the parentheses. If they aren't there, it'll match either itemnum LIKE ... or itemdesc LIKE ... and username = ...
$result = mysql_query( "SELECT * FROM prices WHERE
(itemnum LIKE '%$findit%' ||
itemdesc LIKE '%$findit%') AND username = '$username' ORDER BY price");
The brackets are missing. AND has a higher operator precedence than OR.
I'm tring to convert a Mysql query to using a LIKE clause and I can't make it work.
$query = "SELECT id,name FROM `hin` WHERE name = '".$q."'";
What I've tried in some variations.
$query = "SELECT id,name FROM `hin` WHERE name LIKE %'".$q."'%";
I need the query to select row only on string match. Intend is to use variable as needle.
Use:
"SELECT id,name FROM `hin` WHERE name LIKE '%". $q ."%'"
The wildcarding has to be inside the single quotes.
Ideally, you want to use:
"SELECT id,name FROM `hin` WHERE name LIKE '%". mysql_real_escape_string($q) ."%'"