I'm new in php. I can search through one keyword not by two. I want to search using multiple words e.g "first+experience"
while entry in database feild is as "First_Experience"
I use this query:
$query = "SELECT * FROM fh_Names WHERE fh_Sid LIKE '%$searchTerm%'";
Also this query is for single table, I want to search data through multiple tables. (joining or some otherway). Please tell me query
I mean User name from fh_Name table and its activity from fh_Activity table
Then explode the string and try this
$str = "first+experience";
$expl = explode("+", $str);
$implode = (",", $expl);
$query = "SELECT * FROM fh_Names WHERE fh_Sid IN($implode)";
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...
In my db, i have a table, Timetable where one field is Subject.
It will hold value like this. For example. 1,2,3,4
Now,
I need to compare this field with my array named
$mysubs=array('1','3','5');
I have tried this.
select * from Timetable where Subject IN (".implode(',',$mysubs).");
But if I have only one value in subject field it is working. If it holding something 1,2,3 then it is not working. Is it possible to match using 'LIKE'. Plz help me
Try some thing like this if subject field value is comma separated.
$mysubs = array('1','3','5');
$whereSubClause = '';
for($i=0; $i < count($mysubs); $i++){
$whereSubClause .= 'FIND_IN_SET($i,subject) OR ';
}
// Remove the last OR
$whereSubClasue = substr($whereSubClause,0,-3);
// now query
select * from Timetable where $whereSubClause;
But if it is not comma separated do like this:
$values = implode("','",$mysubs);
"select * from Timetable where subject IN('".$values."');"
$values = implode(',',$mysubs);
$query = "select * from Timetable where Subject IN ($values)";
I use that in some of my projects and it works well
i also thinks it is better to separate query and php operations, at least for a better code readability
Try this query
$sql = "select * from Timetable where Subject LIKE '".implode(',',$mysubs)."%'";
It may help you.
This should work:
$mysubs = array(1, 3, 5);
$sql = "SELECT * FROM Timetable WHERE Subject IN (".implode(', ', $mysubs).");";
That should output this:
$sql = "SELECT * FROM Timetable WHERE Subject IN (1, 3, 5);";
It is generally good practice in SQL to capitalize parts of the language like SELECT, INSERT, or UPDATE. Also your array of values you are searching for in $mysubs were strings, though it is better to have them as integers.
You need to save the field with appending and prepending comma.
For example:
,1,2,3,4, OR ,11,12,23,45,
And you need to search for every array element.
$mysubs=array('1','3','5');
select * from Timetable where Subject LIKE '%,1,%' OR
Subject LIKE '%,3,%' Subject LIKE '%,5,%'
I'm using latin special characters on my database. For exemple:
The word "FLÁVIO" is saved like
flávio
I'm trying to use LIKE to search this item.. for example.. Users search for "Flávio" and I would like to return "flávio".
I tried to do something like this:
$search = $_POST['search'];
$search = htmlentities($search);
$sql = "SELECT * FROM table WHERE column LIKE '%$search%';
The conversion is done, but when I put this $search on the SQL Query it doesn't match.
How can I fix? Thanks.
Just an idea, but have you tried using REGEXP?
$sql = "SELECT * FROM table WHERE column REGEXP '$search'";
I wonder if that will make a difference.
I am trying to create a search blog system where blogs are displayed based on user suggestions. Suggestions are basic hashtags which is stored in user_information table. While blogs will also have some hashtags input by blogger which will be stored in blog table.
Now as I have to match hashtags from user_information table to blog table, I am not finding a way to do this.
I have tried mysql LIKE CLAUSE but I am not able to get even single result.
My Query was [just representation]
SELECT * FROM blog WHERE hashtags LIKE $hashtags_from_user
You are not correctly escaping the value, you require the hash tags to be enclosed in quotations
"LIKE '". $hashtags_from_user ."'";
Additionally you will pay the price in the SELECT statement when you have denormalized database (hash-tags should have their own row in a hash_tags table and the blog would references these via a many-to-many or many-to-one association)
Currently the output of the query is:
SELECT * FROM blog WHERE hashtags LIKE '#hashtag1,#hashtag2,#hashtag'
This isn't going to give you the results you want if you need to match on the individual tags.
The only soultion would be to read the users has tag string, break it up into each tag (using $tags = explode(',', $hash_tags_from_user); and then build up a query to include all of them
$where = array();
$select = 'SELECT * FROM foo';
foreach($tags as $tag) {
$where[] = sprintf('tag LIKE \'%s\'', $tag);
}
if (! empty($where)) $select .= 'WHERE ' . implode(' OR ', $where);
Please don't use the above as it's just and example (and open to simple SQL injection) consider using prepared statements, with PDO/MySQLi.
Try this:
$sql = 'SELECT * FROM blog WHERE hashtags LIKE "'.$hashtags_from_user.'";';
You have to put the string you are searching for between ' or " you can do it without only with numbers.
I have a jQuery list which is returning a list of user_name on php page like
rohit,Bhalu,Ram
Now I want to filter the user_names from the database which is not the part of above list
So far I am trying the basic query of mysql like
select * from table_name where user_name NOTIN('rohit','Bhalu','Ram');
But problem with above query is, this a not the specific solution for bigger list which contains 1000 user_name so I want to use some query filter with php
Please suggest me what should I do in this stage ?
First use index for field user_name.
Second use this query (in $array - usernames)
$array = array('Rohit', 'Bhalu');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
$query = "select * from table_name where user_name NOT IN($comma_separated)";