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) ."%'"
Related
I know how to perform an SQL LIKE % query for a single value like so:
$sql = "SELECT * FROM Farmers WHERE Available_products LIKE ('%Bananas%')";
but how do I do this if the search terms for my LIKE comes from an array? For example, let's say we have an array like this:
$_POST['Products']="Cacaos,Bananas";
$array=implode(',', $_POST['Products']);
$sql = "SELECT * FROM Farmers WHERE Available_products LIKE ('%$array%')";
I want to get all the records in database that the column Available_Products contains Cacaos or Bananas or Both
Convert the array to a regular expression and use REGEX instead of LIKE
$_POST['Products'] = array('Cacaos', 'Bananas');
$regex = implode('|', $_POST['Products']);
$sql = "SELECT * FROM Farmers WHERE Available_products REGEX :regex";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':regex', $regex);
$stmt->execute();
You can try with the help of REGEXP .
$_POST['Products']="Cacaos,Bananas";
$array=implode('|', $_POST['Products']);
$sql = "SELECT * FROM Farmers WHERE
Available_products REGEXP". $array;
You can also do something like :
// Populating join table.
CREATE TABLE temp_table_myArray (myArray nvarchar(255));
INSERT INTO temp_table_myArray (myArray) VALUES ('Cacaos');
INSERT INTO temp_table_myArray (myArray) VALUES ('Bananas');
// Query using the join table.
SELECT F.*
FROM Farmers AS F
INNER JOIN temp_table_myArray AS T ON (F.Available_products LIKE(CONCAT('%',T.myArray,'%')));
But I think there is a better PHP way to solve your problem...
I defined a model and tried to get data from two tables using the UNION keyword. Here I used the LIKE keyword as a constraint. When I use this query without variables (hard-coded variable values), it works. But it doesn't work with variables. Instead it gives an empty array. What's wrong with it?
function searchProf(){
//$name=$this->input->post('name');
$name='du';
$query=$this->db->query("SELECT name FROM users WHERE name like '%".$name."%' UNION SELECT name FROM children WHERE name like '%".$name."%' ");
print_r ($query->result());
}
Change '%".$name."%' to this '%$name%'.
function searchProf(){
//$name=$this->input->post('name');
$name ='du';
$query = $this->db->query("SELECT name FROM users WHERE name like '%$name%' UNION SELECT name FROM children WHERE name like '%$name%' ");
print_r ($query->result());
}
Please use active record base or SQL binding to your SQL queries. Otherwise you have to face for SQL Injections.
`
function searchProf(){
$name ='du';
$sql = "SELECT name FROM users WHERE name like ? UNION SELECT name FROM children WHERE name like ? ";
$query = $this->db->query($sql,array('%'.$name.'%','%'.$name.'%'));
print_r ($query->result());
}
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 am trying to do a query in PHP PDO where it will grab a simple result. So like in my query I need it to find the row where the column group is 'Admin' and show what ever is in the group column. I know that we already know what it should be [Should be admin] but just need to get the query to work. Its only grabbing 1 row from my table, so will I need forsearch?
If I change WHERE group = 'Admin' to WHERE id = '1' it works fine. But I need it so it can be where group = 'admin'
$sql2 = "SELECT * FROM groups WHERE group = 'Admin'";
$stm2 = $dbh->prepare($sql2);
$stm2->execute();
$users2 = $stm2->fetchAll();
foreach ($users2 as $row2) {
print ' '. $row2["group"] .' ';
}
Thanks
group is a reserved word in MySQL, that's why it's not working. In general it's a bad idea to use reserved words for your column and table names.
Try using backticks around group in your query to get around this, so:
$sql2 = "SELECT * FROM groups WHERE `group` = 'Admin'";
Also you should really use placeholders for values, because you're already using prepared statement it's a small change.
Edit: just to clarify my last remark about the placeholders. I mean something like this:
$sql2 = "SELECT * FROM groups WHERE `group` = ?";
$stm2->execute(array('Admin'));
try to use wildcard in your WHERE Clause:
$sql2 = "SELECT * FROM groups WHERE group LIKE '%Admin%'";
Since the value in your table is not really Admin but Administrator then using LIKE and wildcard would search the records which contains admin.
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);