How to take some specific word from string in php? - php

I have a table in mysql that contain two columns or fields, one column is for an id_column and others for description of string type. I would like to insert an specific string to the column description.
For example:
insert into table_name (id, description) values ('1','we go to school')
And then in my php file, I would like to fetch the specific word "school" from the column description.
How can I do this?

You can use like with wildchar .
select * from your_table
where description like '%school%';
in php for find if a string contain a word you can use
$my_string = 'we go to school';
if (strpos($my_strong, 'school') !== false) {
echo 'string found';
}

I'm not really understand what you want. But I guess, you want to get image source from the description column.
$q = mysql_query("select id, description from tabel");
$row = mysql_fetch_assoc($q);
$html = $row["description"];
preg_match( '#src="([^"]+)"#', $html, $match );
$src = array_pop($match);
echo $src;

You'll be needing to use SQL Wildcard Characters
So your query can be something like:
select * from tabel where description like '%school%';
EDIT :
I guess you need to get description first then according to it you want to manage the output.
For that try this :
$sqldata= mysqli_query('select * from tabel)';
$rowcount=mysqli_num_rows($sqldata);
if($rowcount > 0)
{
while($result = mysqli_fetch_assoc($sqldata)) // fetch records
{
$description = $result['desription'];
$id = $result['id'];
}
}

Related

Creating a MYSQL query from results of splitting a string and using LIKE to match keywords in PHP

I am trying to split a string into an array and use these as keywords to make an sql query. I have made a sample of splitting the array and building the sql query. It sort of works but it is giving every table as a result but when I hard copy the built query it comes up with expected results.
This is what I have so far -
The string of keywords split into array and the query is built.
The db is called 'clients_personal' and the table is called 'likes'
$my_search = "paper, glue, discount, bulk";
$new_search = preg_split("/,/", $my_search);
$mmsql = "SELECT * FROM clients_personal WHERE likes LIKE '%offers%'";
foreach ($new_search as $value) {
$mmsql = $mmsql." OR likes LIKE '%$value%'";
}
No that results something like :
$mmsql="SELECT * FROM clients_personal WHERE likes LIKE '%offers%' OR likes LIKE '%paper%' OR likes LIKE '%glue%' OR likes LIKE '%discount%' OR likes LIKE '%bulk%'";
Now if I search like this, I get all the rows in the db?
$sql = "$mmsql";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id=$row["id"];
echo $id;
}
}
But if I query is as hard coded it gives predicted results?
$sql = "SELECT * FROM clients_personal WHERE likes LIKE '%offers%' OR likes LIKE '%paper%' OR likes LIKE '%glue%' OR likes LIKE '%discount%' OR likes LIKE '%bulk%'";
$result = $conn->query($sql);
I have a feeling its to do with quotes and i have tried removing them but no good? Any advice?
Also I an using this type of search as I found it on here.
The problem is here $new_search = preg_split("/,/", $my_search); use $new_search = preg_split("/, /", $my_search); instead.
The items in the string are separated by a comma and a space (", ") so you should split the string with that.

WHERE clause doesn't seem to work in PHP

The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.

How to search based on two cell values

I am using a search box to find entries in my database.
One column contains the title / name of the thing being looked for.
Then I have a second column which gives a short description as applicable (few words usually)
I am trying to search (with the search box) to match either one or even both of the cells combined. Is that possible, and if yes, how?
You can see my code below which kind of shows what I try to achieve, but in any case, let me illustrate this a bit:
Example:
Database.title = "towel"
Database.description = "red, small"
Now, if I type into my search box "tow%red" I want to find the item.
It obviously does not find it, because the title does not contain "red". But if I search for "towel" only, it might show me different items based on different sizes, colors, or other attributes and the list becomes too long.
Of course I also want to be able to simply search for "%red%small% and still be able to find it, or just search for "towel".
Here is my php code that does the search:
if ( !isset($_REQUEST['term']) ) {
exit;
}
$search = mysql_real_escape_string($_REQUEST['term']);
$queryString = "SELECT * FROM items WHERE title+description LIKE '%{$search}%'";
$query = mysql_query($queryString);
$data = array();
if ( $query && mysql_num_rows($query) )
{
while( $row = mysql_fetch_array($query, MYSQL_ASSOC) )
{
$label = $row['title'];
if ($row['description']) {
$label .= ' — '. $row['description'];
}
$data[] = array(
'label' => $label,
'value' => $row['code']
);
}
}
echo json_encode($data);
flush();
I know the above is not going to work, I just put it that way to make clear what I want.
The working code contains this line instead:
$queryString = "SELECT * FROM items WHERE title LIKE '%{$search}%'";
Add a FULLTEXT (see documentation here) index to title and description.
ALTER TABLE items
ADD FULLTEXT INDEX items_index
(title, description);
This will let you search those colums.
Then build a query like this:
SELECT *
FROM items
WHERE MATCH(title, description)
AGAINST ('SEARCH KEYWORD HERE' IN BOOLEAN MODE);
I don't know if that would help you but
"SELECT * FROM items WHERE CONCAT(title, '', description) LIKE '%{$search}%'";
seems simple unless i'm missing something:
$queryString = "SELECT * FROM items WHERE title LIKE '%{$search}%' OR description LIKE '%{$search}%'";
have you tried this.....?
SELECT * FROM items WHERE `title` LIKE '%{$search}%' OR `description` LIKE '%{$search}%'

Compare Column value with more than one in Mysql

I have test table which is
this is my table i am comparing string and getting the entity_id from this table
my sql query for one value search is like this
Select entity_id From test Where BINARY value = '".$my_search_list."'
this works fine if i search for single value like Shirt
i want to search for multiple values and when i try it with Comma Separated value
like Root,Appare,hand Bags (more than work) it is not giving me output
i Also tried with this
Select entity_id From test Where BINARY value IN ( '".$my_search_list."' )
i don't want multiple query i want to do it in single query is it possible???
what i was thinking as i said is to create an array of keywords and see which one match the criteria then show all result.
$search = array('Root','Appare','hand Bags');
$sql = "SELECT `entity_id` FROM `test` WHERE ";
$count = 0;
$search_size = count($search);
foreach($search as $key)
{
$count++;
if($count < $search_size)
{
$sql .= "(`value` = '".$key."') or ";
}
else if ($count == $search_size)
{
$sql .= "(`value` = '".$key."')";
}
}
is you echo $sql you will see the correct query wich should work:
SELECT `entity_id` FROM `test` WHERE (`value` = 'Root') or (`value` = 'Appare') or (`value` = 'hand Bags')`
you need to use quotes for values
SELECT entity_id FROM test WHERE value IN ('one','two','three')

Search mySQL table for string and return the name of the field it is in

I have a table with 3 columns
title | subtitle | body
I need to search all the rows in the table and return which columns contain the search term. How can I do this?
Ultimately the idea is a basic "find & Replace" system.
Something like:
query("SELECT * FROM table WHERE MATCH(title,subtitle,body) AGAINST (?)",
$yourSearchTerm); //> assuming PDO
while($row = fetch_assoc($query)) {
foreach( $row as $k=>$v ) {
if (strpos($v,$yourSearchTerm)!==false)
echo 'The search word was found in the column: '.$k.'<br />';
}
}
If you don't have pdo
mysql_query("SELECT * FROM table WHERE MATCH(title,subtitle,body) AGAINST ('$yourSearchTerm')");
then use
mysql_fetch_assoc();

Categories