LIKE command in Mysql - php

My query:
$sel = mysql_query("select * from categorydetails where productname LIKE '%$commonsearch%' ");
Where product name = tvs apache rtr:
commonsearch = rtr
but it's not working
is there a way to use like command when mysql column contain more than one word?

$sel = mysql_query("select * from categorydetails where productname LIKE '%___rtr%' ");
see the example
SELECT * FROM certificates where csr REGEXP 'tvs'
your query
$sel = mysql_query("select * from categorydetails where productname REGEXP '$commonsearch' ");

Please use below query this is working on single word and multipe string like "test is test"
$sel = mysql_query("select * from categorydetails where productname LIKE '%rtr%' or productname LIKE '%rtr' or productname LIKE 'rtr%' ");

Use this
LIKE '%".$commonsearch."%

Related

Select first letter from firstname only

I have been battling with this for a while now can anyone offer a solution to this.
I want to only show results where the first letter of firstname is "blah".
$n=$_GET['n'];
("SELECT * FROM my_list
WHERE setid = '16-17'
and firstname like '$n%'
order by studentid asc")
The above is what is have so far but shows no results at all.
thanks all
Maybe use mysql's LEFT to get the first character.
SELECT
*
FROM my_list
WHERE
setid = '16-17'
AND LEFT(firstname, 1) = 'x'
ORDER BY
studentid ASC
Will return all firstnames starting with x.
$n = $_GET['n'];
$value = $n[0]; // will give first letter
("SELECT * FROM my_list WHERE setid = '16-17'and
firstname like '". $value ."%' order by studentid asc")

display more that one category from mysql

When I put an item in the database I can chose different "Used" categories .
For example I have Used, Barely Used, Rough. I am wanting all them to display on the same page.
Right now I have this code
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `condition`='Used' $SQLCat $Limit";
Is there a way to do that?
...where (`condition`='Used' OR `condition`='Barely Used' OR `condition`='Rough')...
Try this..
$SQL_GetEquipment = "SELECT * FROM `new_equip` WHERE `condition` LIKE '%Used' OR `condition` LIKE '%Barely Used%' OR `condition` LIKE '%Rough%' $SQLCat $Limit";
Check this link you simply use OR condition it will help you
$SQL_GetEquipment = "SELECT * FROM new_equip WHERE condition='Used' or condition='Barely Used' OR condition='Rough' $SQLCat $Limit";

PHP and Mysql select query issue

Well im working on a small php script and i made a query to connect to db like this:
$id = mysql_real_escape_string(strip_tags($_POST['keyword']));
$query = mysql_query("select * from kalimat WHERE name LIKE '%$id%' OR content like '%$id%'");
while($row=mysql_fetch_assoc($query))
{
echo "<a href='interpretation-".$row['id'].".html'><li>".$row['name']."</li></a>";
}
Now i want to echo the result of select from names then echo the result of selecting from content thank you in advance.
Try that
select * from kalimat WHERE name LIKE '%$id%' OR content like '%$id%'
order by CASE WHEN name LIKE '%$id%' THEN 0
WHEN content LIKE '%$id%' THEN 1 END

Edit php string

I have a string that makes a function from my order status. I wan´t to have it to trigger with 2 order status´
Is that posible?
$query = "SELECT * FROM #__redshop_orders where order_payment_status ='Paid' and order_status = 'RD2'";
So insted of just trigger with status RD2 it should trigger with RD1+RD2
Plus - I wan´t to remove the order_payment_status function, so it only triggers for order status.
How will the string look like after editting?
Thank you.
It would be something like this:
$query = "SELECT * FROM #__redshop_orders WHERE order_status = 'RD1' OR order_status = 'RD2'";
You should try to learn MySQL, a simple google search would give you plenty of example and tutorials.
EDIT:
I made some tests since I posted my answer, and the benchmarks show that a much faster solution would be:
$query = "SELECT * FROM #__redshop_orders WHERE order_status IN ('RD1', 'RD2')";
$query = "SELECT * FROM #__redshop_orders WHERE order_status IN ('RD1', 'RD2')";
$query = "SELECT * FROM #__redshop_orders where order_status IN('RD1','RD2')";
//You want this

PHP / SQL Query and PHP Variables

I have this query below:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id='someid'
ORDER BY id ASC
LIMIT $display_num",$myconn);
see this line: AND id='someid' <-- someid ...
OK, the query above returns 2 results as expected...
Now for the problem....
-- I have a variable myVar and it's content is "someid" (without quotes)...same as the string 'someid'
When I do this:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id=myVar
ORDER BY id ASC
LIMIT $display_num",$myconn);
See: myVar <-- this variable contans .. someid
The second query returns no results.
Update: When using ... AND id='$myVar' it sees $myVar as empty for some reason.
Put a $ in front of myVar:
$msgg = mysql_query(
"SELECT *
FROM mytable
WHERE time > $time
AND id = '$myVar'
ORDER BY id ASC
LIMIT $display_num", $myconn
);
You forgot the dollar sign and the single quotations:
AND id='$myVar'
Additionally, you may want to consider using heredoc:
$query = <<<MYSQL
SELECT *
FROM mytable
WHERE time>$time
AND id='$myVar'
ORDER BY id ASC
LIMIT $display_num
MYSQL;
$msgg = mysql_query($query, $myconn);

Categories