I am facing some problems in MySQL query in PHP.
I have the below Bengali words in a column named 'keywords' and they are assigned to 'গৌরিপ্রসন্ন মজুমদার' value of another column named 'name' in a table named 'lyricist'
সয়না,রয়না,কেমন,তাকে,আমার,তুমি,কৃষ্ণ,রাধে,দিতে,চাই,আরো,কাছে,তোমার,আমার,তুমি,থাকো,কাছে,ডাকো,আমি,পড়েছে
When I'm searching for some consecutive words like 'তোমার,আমার,তুমি' I get the correct answer 'গৌরিপ্রসন্ন মজুমদার' But when i'm searching for random words like 'তোমার,তুমি' it's not giving any answer.
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";
Here $val has 'তোমার,আমার,তুমি' or 'তোমার,তুমি' these type of values.
Why not it's treating the item randomly? Please HELP.
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";
Will get you all the matching columns that start with some text having your value and then end with some text.So if you put some random words and they are not in the correct order obviously this query will not match.
I think what you want to do is when you have multiple values is put them in different variable val1,val2,... and using AND in your query
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val1%' AND keywords LIKE '%$val2%'";
So you would have to construct your query dynamically something like this would work:
<?php
$val="keyword1,keyword2,keyword3";
$keywords=explode(',',$val);
if(!empty($keywords)){
$query="SELECT name FROM `lyricist` WHERE keywords LIKE '%$keywords[0]%' ";
if(count($keywords)>1){
for($i=1;$i<count($keywords);$i++){
$query.="AND keywords LIKE '%$keywords[$i]%' ";
}
}
}
?>
Related
I'm currently attempting to select a specific word from a string based on surrounding conditions. Specifically, I'd like to select a word from a MySQL Query.
For example:
Select the word/words that is in surrounded by tildes (`): I am an `apple` -> apple;
SELECT `data` FROM `tableName` -> data -OR- {"data", "tableName"}; (I mostly need to output "data", but I can work around if I have to select everything in tildes.)
I have a query function that currently needs 2 parameters (to make it simpler to store database values in variables).
function MysqlQuery($query, $find)
{
$query = MysqlQueryWild($query);
$fetch = $query->fetch_array();
// Used to run a standard select query.
return $fetch[$find];
}
I'd like to chop it down to only needing one parameter, the query, and automatically selecting the column within the function with the above method.
Closest I got was this:
function MysqliQuery($query)
{
preg_match('"([^\\`]+)"', $query, $result);
$query = MysqlQueryWild($query);
$fetch = $query->fetch_array();
// Used to run a standard select query.
return $fetch[$result[0]];
}
But it selects the word "SELECT" which is NOT encased by tildes.
In sql you can use the keyword "Like" to find entries with specified conditions. % is used to represent 0-inf characters and _ represents 1 character. So for your example this is the select query you want.
SELECT 'data'
FROM 'table name'
WHERE 'column name' LIKE '`%`';
So for your case just use this query instead of trying to get all the words and manually find the ones surrounded by tildes
I am working on a search filter that connects to a MySQL database. It accepts a keyword parameter; however, it only searches for the keyword in the order it is typed in. For example: If I type in "house rental", it looks for the term in xyz column in the order it is typed it.
However, I would like to change it so that it searches for both those terms are independent of the order they are typed in. Example, if typed in "house rental", the result should contain listings that have either, "house rental" or "rental house" mentioned somewhere in the xyz columns.
I have tried to break the keywords, put it in an array and do a foreach loop on the array to get the result but it generates correct but undesired results. The results that are generated are not the ones that I required.
$samp_text = 'House Rental';
$split_string_array = preg_split('/[\s,]+/', $samp_text);
foreach ($split_string_array as $each_sql_query) {
print_r('SELECT * FROM XYZ WHERE $keyword LINK = %' . $each_sql_query . '% '. "\r");
}
I would like a suggestion on how to tackle this problem.
While the method Sloan Thrasher suggests works, it does not scale.
The solution is to create a table contains each keyword from a searchable document as a single row with a foreign key to the original document with an index on the search word then the foreign key. Split your search term into an equivalent table, join the 2 and count the matches:
Select doc.txt, count(*)
From doc
Inner join keywords
On doc.id=keywords.doc_id
Inner join search
On keywords.word=search.word
Where search.query_id=?
Group by doc.txt
Order by count(*) desc
Alternatively just use the built-in fulltext capability of mysql.
You can build a query that will check for all keywords at once and give you list of rows that contain all of the keywords.
If you want all rows that contain one or more keywords, change the AND in the implode function to OR.
$samp_text = 'House Rental';
$split_string_array = preg_split('/[\s,]+/', $samp_text);
$qstr = "SELECT * FROM XYZ WHERE ";
$keywords = array();
foreach ($split_string_array as $each_sql_query) {
$keywords[] = " LINK = '%" . $each_sql_query . "%'\r")
}
$qstr .= implode(" AND ",$keywords);
// Code to execute query and use results.
I have a column in mysql db that contain multiple ids separated by a comma like so... 100,112,324 . The column is defined as varchar.
I want to find those rows whose ids column contains the id 112 or some other id that I specify. Similar questions I have found suggest splitting up the values in the column using lengthy code. Maybe I'm wrong but I would think there is a cleaner approach. Here is what I'm attempting to do, any help is appreciated...
<?php
include "../connect.php";
$usrid = "%112%";
$stm = $conn->prepare("SELECT * FROM subjects WHERE ids LIKE ?");
$stm->execute($usrid);
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['consultant']."<br>";
}
?>
I get a blank page as is.
Look at this PDO prepared statement:
Try putting the "to be binded variable" in an array:
$stm->execute(array($usrid));
use single quotes for LIKE query, because you have data type as varchar.
Try this:
$stm = $conn->prepare("SELECT * FROM subjects WHERE ids LIKE '?'");
$stm->execute($usrid);
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 have a column of pony names, where the breeder's Prefix is included with the pony's name (eg. Ashbrook Boy, where Ashbrook is the Breeder's Prefix, and Boy is the name of the pony). I have another table where I have a list of all the Prefixes used. I want to cycle through that list, and for each record search through my ponies and fetch those whose names begin with each prefix in turn. When they are fetched, I want to remove the said prefix from their name, and pop it into a column for that purpose on their own table.
In the end, I want -rather than one column with both Prefix and Name mixed in - two columns: one for Prefix, one for Name.
I thought the code below would do it for me, but it's not working. I get a 'not a valid resource' error for $res. Any help you could give me would be hugely appreciated - I really don't want to do this by hand! :P
I'm using a PHP script off a MySQL db, which I can access via PHPMyAdmin.
include '../conn.php';
$q=mysql_query("SELECT DISTINCT Pre FROM prefixes");
while($r=mysql_fetch_array($q)) {
$pre=$r['Pre'];
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
mysql_query($sql);
echo $sql;
while($res=mysql_fetch_array($sql)){
$name=$res['Name'];
$name=trim(str_replace("$pre","", $name));
$id=$res['ID'];
mysql_query("UPDATE profiles SET Prefix = '$pre', Name = '$name' WHERE ID = '$id' ");
}
}
mysql_close($con);
Your mistake come from the fact that $sql is the query string, not the mysql result of the query
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
mysql_query($sql);
echo $sql;
while($res=mysql_fetch_array($sql)){
with this, it will look better :
$sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
$result = mysql_query($sql);
while($res=mysql_fetch_array($result)){