php blocked words from mysql database - php

I need to save a list of blocked words in mysql database and use my below code to block some words or phone numbers... Please help with a little example.
Thanks
$blocked_words=array("word1" ,"word2","word3");
$string_words=explode(" ", $_POST['text']);
$phone_words=explode(" ", $_POST['phone']);
$result = array_intersect($blocked_words, $string_words);
$result_phone = array_intersect($blocked_words, $phone_words);

You can generate an array from the blocked_words table and use it like above code you mentioned,
Or
May be what you think you want is something similar to
"SELECT word FROM blocked_words WHERE word == $Phone_num"
and check whether row count is 0 or not for determining whether it is blocked word?

I think i was not able to explain, but i have completed my requirement from one of a stackoverflow answers from another post
here is what i was required
$blocked_words=array();
$q="select words from blocked";
$rs=mysql_query($q);
while($rd=mysql_fetch_object($rs))
{
$blocked_words[]=$rd->words;
}
by this i got all words from table 'blocked' into $blocked_words, now i can use it further
Thanks

Related

PHP: trying to make a search engine for my website

I am trying to make a search engine for my website. It works pretty good if I enter a single word as search query like foo or bar. The problem is it fails when I enter two or more words like foo bar.
How should I deal with such words? Splitting the words (using explode) and then look for each word in the table for indexed words? This doesn't sound like a good idea
I am using GET method for this search engine.
<?php
if($_GET['query']){
$search = $_GET['query'];
$search = strip_tags($search);
$search = trim($search);
$search = addslashes( strtolower($search));
echo $search;
$start = microtime();
$searchquery = mysql_query("SELECT sw_wordid FROM tbl_search_word WHERE sw_word LIKE '%$search%'");
//tbl_search_word contains all the indexed words
$searchrow = mysql_fetch_array($searchquery);
$end = microtime();
echo $searchrow['sw_wordid'].'<br />';
echo $end - $start;
}
?>
First of all, I agree with Hanky Panky splitting it is not by itself a bad option you can try that, but as nobody else help you with some other alternative I give you one more, it's not so effective when searching many keywords and depending on what you want as a result.
First of all I beg you properly santinize properly your input DO NOT just put what the user gives you in the string like the code you posted, never do that, you will be probably opening a security hole for sql inyection.
Try prpared statments using either PDO or mysqli.
Can you try this:
$searchquery = mysql_query("
SELECT sw_wordid FROM tbl_search_word WHERE INSTR(sw_word,'$search')>0
");
And you can share your benchmark with use to help you improve it but not knowing exactly what you want for the result, the alternative you choose may depend on it.
Please share your results.
Hope this helps you.
I guess the space you entered is taken as %20 so an error occurs why not use str_replace("%20", " ",$query);
hope it helps

how to implement the an effective search algorithm when using php and a mysql database?

I'm new to web design, especially backend design so I have a few questions about implementing a search function in PHP. I already set up a MySQL connection but I don't know how to access specific rows in the MySQL table. Also is the similar text function implemented correctly considering I want to return results that are nearly the same as the search term? Right now, I can only return results that are the exact same or it gives "no result." For example, if I search "tex" it would return results containing "text"? I realize that there are a lot of mistakes in my coding and logic, so please help if possible. Event is the name of the row I am trying to access.
$input = $_POST["searchevent"];
while ($events = mysql_fetch_row($Event)) {
$eventname = $events[1];
$eventid = $events[0];
$diff = similar_text($input, $event, $hold)
if ($hold == '100') {
echo $eventname;
break;
else
echo "no result";
}
Thank you.
I've noticed some of the comments mentioned more efficient ways of performing the search than with the "similar text" function, if I were to use the LIKE function, how would it be implemented?
A couple of different ways of doing this:
The faster one (performance wise) is:
select * FROM Table where keyword LIKE '%value%'
The trick in this one is the placement of the % which is a wildcard, saying either search everything that ends or begins with this value.
A more flexible but (slightly) slower one could be the REGEXP function:
Select * FROM Table WHERE keyword REGEXP 'value'
This is using the power of regular expressions, so you could get as elaborate as you wanted with it. However, leaving as above gives you a "poor man's Google" of sorts, allowing the search to be bits and pieces of overall fields.
The sticky part comes in if you're trying to search names. For example, either would find the name "smith" if you searched SMI. However, neither would find "Jon Smith" if there was a first and last name field separated. So, you'd have to do some concatenation for the search to find either Jon OR Smith OR Jon Smith OR Smith, Jon. It can really snowball from there.
Of course, if you're doing some sort of advanced search, you'll have to condition your query accordingly. So, for instance, if you wanted to search first, last, address, then your query would have to test for each:
SELECT * FROM table WHERE first LIKE '%value%' OR last LIKE '%value%' OR address LIKE '%value'
Look at below example :
$word2compare = "stupid";
$words = array(
'stupid',
'stu and pid',
'hello',
'foobar',
'stpid',
'upid',
'stuuupid',
'sstuuupiiid',
);
while(list($id, $str) = each($words)){
similar_text($str, $word2compare, $percent);
if($percent > 90) // Change percentage value to 80,70,60 and see changes
print "Comparing '$word2compare' with '$str': ";
}
You can check with $percent parameter for how strong match you want to apply.

how to updated the keywords from database

hi i have stored 1000 keywords in my database . if i search any keyword(with in my database) my site title must come Like a AIRPORT NETWORKS this title i want . this is for search engine box. how can i do with sql queries i used that below query for displayed my site title.
$ConvertedResultArray = explode('<div id="resultsDiv">', $ConvertedResult);
$V1 = $ConvertedResultArray[0];
$V2 = $ConvertedResultArray[1];
$SponsoredContent = '';
if(strtolower($SearchQuery) == 'taxi')
{
$SponsoredContent = '<br />AIRPORTS<br />NETWORKS';
}
$ConvertedResult = "$V1$SponsoredContent$V2";
i have a only one table named keywords
if i entered that key "taxi" in search box That title comes infront of the page AIRPORTNETWORKS as like that if i entered in the whole 1000 words which it is stored in database it must be come .
how can i do that what sql query i have to use.is it possible. please help me if any one have an idea thanks in advance
I realise this not the answer, but here is something to get you started
SELECT *
FROM Keywords
WHERE Name LIKE "%AIRPORT%"
You need to use AJAX for that. I guess you are speaking about auto complete. if is tat you are talking about do the following steps.
1. use the query given by PerformanceDBA
2. update the textbox value with the first row of query result by triggering textbox onkeyup() event.
If this is not what you want please rephrase your question so tat others can understand..

only retrieving 1 row of data from MYSQL database

I am trying to input multiple pieces of data through a form and all the data will be separated by (,). I plan to use this data to find the corresponding id for further processing through an sql query.
Below is the code I use.
$key_code = explode(",", $keyword);
//$key_count = count($key_code);
$list = "'". implode("','", $key_code) ."'";
//$row_count = '';
$sql4= "SELECT key_id FROM keyword WHERE key_code IN (".$list.")";
if(!$result4 = mysql_query($sql4, $connect)) {
mysql_close($connect);
$error = true;
}else{
//$i = 0;
while($row = mysql_fetch_array($result4)) {
$keyword_id[] = $row['key_id'];
//$i++;
}
//return $keyword_id;
}
The problem i see is that keyword_id[0] is the only element that contains any data (the data is accurate). Even if I input multiple values through the aforementioned form.
I thought it might be an error in the sql but I echo'ed it and it looks like:
SELECT key_id FROM keyword WHERE key_code IN ('WED','WATER','WASTE')
The values in the brackets are exactly what I inputted.
I even tried to figure out how many rows are being returned by the query and it shows only 1. I assume something is wrong with my query but I cannot figure where.
Any help will be greatly appreciated.
Edit: Alright Solved the problem. Thanks to suggestions made I copied and pasted the $sql_query I had echo'ed on the website into mysql console; which resulted in only 1 row being retrieved. After taking a closer look I realized that there was a whitespace between ' and the second word. I believe the problem starts when I input the key_code as:
WED, WATER, WASTE
Instead inputting it as
WED,WATER,WASTE
fixes the problem. I think I should make it so that it works both ways though.
Anyway, thank you for the help.
I am pretty sure that the query is ok. How many rows do you get with just
SELECT key_id FROM keyword
I think that there is just one line that matches your WHERE.
Check the query directly in the database(with phpmyadmin, or in the mysql console), however this query seems to be working as you may assumed. If it returns only 1 row when you use it directly in the db, then maybe there is only one row in your table wich matches this query.

MySQL: How to search for spelling variants? ("murrays", "murray's" etc)

I want to search like this: the user inputs e.g. "murrays", and the search result will show both records containing "murrays" and records containing "murray's". What should I do in my query.pl?
What do you think about using the SOUNDEX function and the SOUNDS LIKE operator ?
That way, you can simply do:
SELECT * from USERS WHERE name SOUNDS LIKE 'murrays'
I'm pretty sure it doesn't work for every case, and perhaps it is not the most efficient way to solve the problem, but it could fit your needs.
This won't help if you absolutely need to do these queries in SQL, but if you can set up a Lucene search index for it, you gain a lot of this kind of "fuzzy search" functionality. Note though that Lucene is quite a complex topic by itself.
What you could do is create an extra field in the database, which contains the data with all special characters stripped from it, and search there. A bit lame, I know. Looking forward to see smarter answers ;)
Quick and dirty:
SELECT * FROM myTable WHERE REPLACE(name, '\'', '') = 'murrays'
I would first build a search column which has the text without punctuation and then search on that. Otherwise you'll have have to have a series of regular expressions to search against or check individual records in PHP for matching: both of which are computational intensive operations.
Maybe something like this: (untested!)
SELECT * FROM users WHERE REPLACE(user_name, '\'', '') = "murrays"
If this is for single word searching, you could try using Soundex or Metaphone functions? These would handle sounds-like as well as spelling
Not sure if MySQL has these, but PHP does (which would require separate columns to hold these values).
Otherwise, Richy's no-punctuation extra column seems best.
You could try adding a replace to your query like this
replace(name, '''','')
to temporarily get rid of the apostrophes for the match.
select name from nametable where name = replace(name,'''','');
This query should be able to pick up "murrays" or "murray's".
var inputStr = "murrays";
inputStr = String.Replace("'", "\'", inputStr);
SELECT * FROM ATable WHERE Replace(AField, '\'', '') = inputStr OR AField = inputStr
strip user input and names in database from all non-letter characters.
Use levenstein distance or soundex to find murrays with murray or marrays. This is optional but your users would love that.

Categories