I have concatenated three tables to one table that is edit all. Now i'm going to retrieve the all the 4 values that starts with the particular letter, But is displaying all values with starts with different letter.
include "config.php";
$term = strip_tags(substr($_POST['searchit'],0,100));
$term = mysql_real_escape_string($term);
if($term=="")
echo "Enter Something to search";
else{
$query = mysql_query("select distinct videoname,category,name,email from edit_all
where videoname like '$term%' or category like '$term%' or name like '$term%' or email like '$term%'limit 1") or die(mysql_error());
$string= '';
if (mysql_num_rows($query))
{
while($row = mysql_fetch_assoc($query)){
$string .= "<b><a href='#'>".$row['videoname']."</a></b> -<br> ";
$string .= $row['category']."-<br>";
$string .= $row['name']."-<br>";
$string .= $row['email']."-<br>";
$string .= "\n";
}
}
else{
$string = "No matches found!";
}
echo $string;
}
?>
Related
I create a simple search form. Firstly I explode words and make some query (for search multiple words at once). Also, I want to search only in Movie titles. Movie titles can be found in movie_data.
My database called movies have rows:
id and
movie_data - contains movie data in JSON. Example storaged data in json:
{"movie_title":"Forrest Gump","movie_cover":"http://1.fwcdn.pl/po/09/98/998/7314731.6.jpg","movie_name_en":"","movie_desc":"Historia życia Forresta, chłopca o niskim ilorazie inteligencji z niedowładem kończyn, który staje się miliarderem i bohaterem wojny w Wietnamie.","movie_year":"1994","movie_genres":"DramatKomedia"}
Here is my code. This code don't return any results.
$query = 'Forrest Gump';
$words = explode(' ', $query);
$i = 0;
$len = count($words);
$query_build = '';
foreach ($words as $item) {
if ($i == $len - 1) {
$query_build .= ' titlemov LIKE "%$item%"';
} else {
$query_build .= ' titlemov LIKE "%$item%" OR ';
}
// …
$i++;
}
$sql = "SELECT movie_data, JSON_EXTRACT(movie_data, '$.movie_title') AS titlemov
FROM movies WHERE $query_build";
This code don't result any items
UPDATE
I updated my code.
$query = 'Forrest Gump';
$words = explode(' ', $query);
$i = 0;
$len = count($words);
$query_build = '';
foreach ($words as $item) {
if ($i == $len - 1) {
$query_build .= " JSON_EXTRACT(movie_data, \'$.movie_title\') LIKE '%$item%'";
} else {
$query_build .= " JSON_EXTRACT(movie_data, \'$.movie_title\') LIKE '%$item%' OR ";
}
// …
$i++;
}
$sql = "SELECT * FROM movies WHERE $query_build";
Now i getting error:
Warning: mysqli::query(): (22032/3141): Invalid JSON text in argument 1 to function json_extract: "Missing a comma or '}' after an object member." at position 228. in /search.php on line 43
Line 43:
$result = $conn->query($sql);
var_dump of $sql var:
SELECT * FROM movies WHERE JSON_EXTRACT(movie_data, '$.movie_title') LIKE '%Forrest%' OR JSON_EXTRACT(movie_data, '$.movie_title') LIKE '%Gump%'
Hmm?
titlemov doesn't exist for your where clause to use. What you want to use inside your where clause (specified in the $query_build) is the JSON_SEARCH function. Here's what it looks like in general with wildcards for the movie name:
SELECT movie_data, movie_data->'$.movie_title' AS titlemov FROM movies
WHERE JSON_SEARCH(movie_data->'$.movie_title', 'one', "%orrest Gum%") IS NOT NULL;
You'll just need to modify your $query_build string. I also drop the needless JSON_EXTRACT function calls.
Try this:
$query_build = " JSON_SEARCH(movie_data->'$.movie_title', 'one', '%$item%') IS NOT NULL";
Final note, I'm assuming $item is not a user supplied string, otherwise this opens you up to SQL injection. If it is supplied by user, you'll want to use a prepared statement and bind this value.
Below is my query to show user comments. It works well:
enter
$query='SELECT
customers.cust_id,
customers.f_name,
customers.l_name,
user_comments.comment_txt,
clubs.name_club
from
customers
inner join
user_comments
on customers.cust_id = user_comments.cust_id
inner join
clubs
on user_comments.cust_id = clubs.cust_id
ORDER BY RAND() LIMIT 1,1
';
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$nameclub =$row['name_club'];
$comment =$row['comment_txt'];
echo '<div><img src="images/arrow.gif"> Name : '.$nameclub.'</div>';
echo '<div>'.$comment.'</div><br>';
echo '<div>'.$row['f_name'].' '.$row['l_name'].'</div>';
}
echo '<p style="text-align: left"> Show all comments</ ></p> '; ?>
I now want to show just about 100 characters of a user comment. So I changed my query accordingly, but it does not work:
enter $query='SELECT
customers.cust_id,
customers.f_name,
customers.l_name,
user_comments.comment_txt.substr(comment_txt,1,100) as comment_txt,
clubs.name_club
from
customers
inner join
user_comments
on customers.cust_id = user_comments.cust_id
inner join
clubs
on user_comments.cust_id = clubs.cust_id
ORDER BY RAND() LIMIT 1,1
';
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$nameclub =$row['name_club'];
$comment =$row['comment_txt'];
echo '<div><img src="images/arrow.gif"> Name : '.$nameclub.'</div>';
echo '<div>'.$comment.'</div><br>';
echo '<div>'.$row['f_name'].' '.$row['l_name'].'</div>';
}
echo '<p style="text-align: left"> Show all comments</ ></p> '; ?>
How can I fix my query to just get 100 characters of a comment?
You're using SUBSTR() incorrectly. Replace this:
user_comments.comment_txt.substr(comment_txt,1,100) as comment_txt,
With this:
SUBSTR(user_comments.comment_txt,1,100) as comment_txt,
Edit per comment: to add ... if the text continues past 100 characters, use
CONCAT(SUBSTR(user_comments.comment_txt,1,100), IF(LEN(user_comments.comment_txt) > 100, '...', '')) as comment_txt,
Try this
Replace this
user_comments.comment_txt.substr(comment_txt,1,100) as comment_txt,
To
SUBSTRING(user_comments.comment_txt,1,100) as comment_txt,
Mysql function_substring
Another solution is to use a php function that cuts a string into a number of words:
<?php
function cut_string($string_to_cut, $number_words, $append = "...") {
$string = array();
$string_return = "";
$return_string = "";
$string = explode(" ", $string_to_cut);
if(count($string) < $number_words) {
for($x=0; $x < count($string); $x++) {
$string_return .= $string[$x] . " ";
}
$return_string = substr($string_return, 0, -1);
}
else {
for($x=0; $x < $number_words; $x++) {
$string_return .= $string[$x] . " ";
}
$string = substr($string_return, 0, -1);
$return_string = $string . $append;
}
return($return_string);
}
?>
Note: This is word count based, not character count based.
I am using a search box that searches the user input from "title". Besides this i want to count some keywords from another column "subject" only in the presense of user input corresponds in "title" column. but the problem is that how to find count of only words i have mentioned in the query i.e.
Title Subject
a word1,word2,word1,
ab word2,word4,word3,
bb word1,word4,word4,
aa word2,word2,word4,
cb word1,word1,word3,
ac word2,word1,word3,
So, here if i have searched for a in textbox then, how can i get count of word1 from subject where only a is present in the title field and then similar for the other words so that i could get output like:
your search a contains :
word1 (3)
word2 (5)
word3 (2)
word4 (2)
The code for query is given below:
$getq = "SELECT Title,Subject COUNT(*) FROM tablename WHERE Title LIKE '%$search_each%' Subject LIKE '%word1%' OR Subject LIKE '%word2%' OR Subject LIKE '%word3%' OR Subject LIKE '%word4%') GROUP BY Subject";
$getquery = $conn->query($getq);
while( $runrows = mysqli_fetch_assoc($getquery))
{
$sub = $runrows ['Subject'];
$countsub = $runrows ['COUNT(*)'];
}
echo "<a href='#'>"word1"(".$countsub.")</a> ";
echo "<a href='#'>"word2"(".$countsub.")</a> ";
echo "<a href='#'>"word3"(".$countsub.")</a> ";
echo "<a href='#'>"word4"(".$countsub.")</a> ";
Here, $search_each is textbox input and the above code shows nothing in place of $countsub because i don't know how to get individual count for all words under presense of user's search term. How this can be done?.
Your help will be highly appreciated.
Try this:
$z = array("word1", "word2", "word3", "word4", "word5");
$countsub1 = array();
for ($i = 0; $i <= 4; $i++) {
$getq3 = "SELECT COUNT(*) FROM table WHERE (Subject LIKE '%$z[$i]%' AND Title LIKE '%$search_each%') ";
$getquery4 = $conn->query($getq3);
while ($runrows = mysqli_fetch_assoc($getquery4)) {
$sub = $runrows['Subject'];
$countsub = $runrows['COUNT(*)'];
}
$countsub1[$i] = $countsub;
echo "<a href='#' >$z[$i](" . $countsub1[$i] . ")</a><br> ";
}
Don't write links outside the loop, this can solve your problem.
I have the following PHP search script.
When I search for 1234567, it matches the exact phrase but I want it to match only initial 4 characters i.e. 1234.
Explanation:
I want it to count the initial 4 characters and display the result matching those initial characters. E.g. if someone search for 1234567 then the script should count the initial 4 characters i.e. 1234 and show the results. Similarly if someone search 456789 then the script should count the initial 4 characters i.e. 4567 and show the results.
///explode search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each)
{
//construct query
$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";
}
//echo out construct
$construct = "SELECT * FROM numbers WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "No results found.";
{
echo "$foundnum results found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))
{
//get data
$title = $runrows['title'];
$desc = $runrows['description'];
$url = $runrows['url'];
echo "<b>$title</b>
<b>$desc</b>
<a href='$url'>$url</a><p>";
You can replace your foreach with:
foreach($search_exploded as $search_each)
{
$str = mysql_real_escape_string(substr($search_each, 0, 4));
//construct query
$x++;
if ($x==1) $construct .= "keywords LIKE '$str%'";
else $construct .= " OR keywords LIKE '$str%'";
}
To modify your existing code to only use the first 4 words, you just need to change your loop a bit:
Change this-
foreach($search_exploded as $search_each) {
To this -
for($i = 0; $i < min(count($search_exploded), 4); ++$i) {
$search_each = $search_exploded[$i];
Add the following code to the top of your foreach loop:
if(strlen($search_each) > 4) $search_each = substr($search_each, 0, 4);
Like this:
$search_exploded = explode(" ", $search);
foreach($search_exploded as $search_each)
{
if(strlen($search_each) > 4) $search_each = substr($search_each, 0, 4);
// your code
}
This code will search for first 4 characters in every word if that word is longer than 4.
The search engine I'm using is a MySQL/PHP search engine.
I have been searching the forums for some time to try to get a solution for this problem.
If I have keywords for 3 separate images labeled:
#1 pure black
#2 pure white
#3 pure green
If I type "pure white" in the search box all 3 images come up because they all share the word "pure".
Is there a workaround or code I can add to only match exact search phrases when using multiple search keywords?
This is the code:
<?php
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$s = $_GET['s'];
if (!$s)
$s = 0;
$e = 10;
$next = $s + $e;
$prev = $s - $e;
if (strlen($search) <= 2)
echo "Must be greater then 3 chars";
else {
echo "<br /><table><tr><td><img src='juzzy.jpg' /></td><td><form action='search.php' method='GET'><input type='text' onclick=value='' size='50' name='search' value='$search'> <input type='submit' name='submit' value='Search'></form></td></tr></table>";
//connect to database
mysql_connect("","","");
mysql_select_db("");
//explode out search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each) {
//construct query
$x++;
if ($x == 1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";
}
//echo outconstruct
$constructx = "SELECT * FROM searchengine WHERE $construct";
$construct = "SELECT * FROM searchengine WHERE $construct ORDER BY keywords LIMIT $s,$e ";
$run = mysql_query($constructx);
$foundnum = mysql_num_rows($run);
$run_two = mysql_query("$construct");
if ($foundnum == 0)
echo "No results found for <b>$search</b>";
else {
echo "<table bgcolor='#0000FF' width='100%' height='1px'><br /></table><table bgcolor='#f0f7f9' width='100%' height='10px'><tr><td><div align='right'>Showing 1-10 of <b>$foundnum</b> results found for <b>$search.</b></div></td></tr></table><p>";
while ($runrows = mysql_fetch_assoc($run_two)) {
//get data
$title = $runrows['title'];
$desc = $runrows['description'];
$url = $runrows['url'];
echo "<table width='300px'>
<h4><a href='http://$url'><b>$title</b></a><br />
$desc<br>
<font color='00CC00'>$url</font></table></h4>";
}
?>
<table width='100%'>
<tr>
<td>
<div align="center">
<?php
if (!$s<=0)
echo "<a href='search.php?search=$search&s=$prev'>Prev</a>";
$i =1;
for ($x=0;$x<$foundnum;$x=$x+$e) {
echo " <a href='search.php?search=$search&s=$x'>$i</a> ";
$i++;
}
if ($s<$foundnum-$e)
echo "<a href='search.php?search=$search&s=$next'>Next</a>";
}
}
?>
</div>
</td>
</tr>
</table>
Don't explode your search string. This will force it to use the full search string as a search, rather than any one of the pieces.
Or, you could change your operator OR in your construct to AND, and that way it would require all the words to appear, regardless of order, however.
Don't use fuzzy LIKE %% queries, query by keywords = '$search'.