I'm trying to retrieve records in the order in which I think they are being accessed.
My code is as follows:
the data to search for:
$to_check="Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs";
the select statement:
$sql = "SELECT wd, wd_ps2, rt1, rt4, definition FROM august_2022 WHERE wd_ps2 IN ($to_check) order by ".$to_check."";
and the outcome is (I've put it in list form to make it easier to see v the original):
Yes_ij_affirmation
there_px_ex
._fs (should be last)
,_cm (should be second)
is_vbz_1 (should be third)
I'm not sure whether what I am trying to do is feasible, but would welcome advice.
WW
You need to add quotes in
$to_check
$to_check="'Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs'";
And the select statement should look like this:
$sql = "SELECT wd, wd_ps2, rt1, rt4, definition FROM august_2022 WHERE wd_ps2 IN ($to_check) order by " .$to_check;
You need quotes at the beginning and end of $to_check.
Then you can use the FIELD() function to order by the position in that list.
$to_check="'Yes_ij_affirmation', ',_cm', 'there_px_ex', 'is_vbz_1', '._fs'";
$sql = "SELECT wd, wd_ps2, rt1, rt4, definition
FROM august_2022
WHERE wd_ps2 IN ($to_check)
order by FIELD(wd_ps2, $to_check)"
Related
I have a search engine which is pretty straight forward. The query is below.
$sql = "SELECT * FROM events WHERE eventname LIKE '%".$_POST["search"]."%'
OR place LIKE '%".$_POST["search"]."%'
OR country LIKE '%".$_POST["search"]."%'
OR date LIKE '%".$_POST["search"]."%'
LIMIT 40";
But, Problem with this is this,
if I put the 'eventname' in search-box it is okay and it saws data from eventname column correctly. Or if I search only for place or country or date individually, it shows data correctly. But, if I search (FOR EXAMPLE) for both eventname and place together search results shows nothing. Using this query what are the changes I have to make to get it working?
Additionally I want to say that I have seen some of the query like "MATCH ... AGAINST". Though I don't want to use that, but if there are no other way what could be that "MATCH...AGAINST" query for this?
Here is my full code. They are straight forward. And I am working on a weird client's project and he want it to be like this and security is not a fact for him. So, you might notice some security issue which will be solved later. but the query first.
<?php
include_once("admin/connection/db.php");
$output = '';
$sql = "SELECT * FROM events WHERE eventname LIKE '%".$_POST["search"]."%'
OR place LIKE '%".$_POST["search"]."%'
OR country LIKE '%".$_POST["search"]."%'
OR date LIKE '%".$_POST["search"]."%'
OR date AND country LIKE '%".$_POST["search"]."%'
OR date AND place LIKE '%".$_POST["search"]."%'
OR date AND eventname LIKE '%".$_POST["search"]."%'
LIMIT 40";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$output .= "<a class='search-result' href='".$row["link_pdf"]."'><li><i class='fa fa-trophy'> </i> ".$row["eventname"].'<br/>'.date('Y-m-d', strtotime($row["date"])).' || '.$row["place"].', '.$row["country"].'<img src="logos/'.$row["country"].'.png" width="30px" height="18px;" /></li></a>';
}
echo $output;
}else {
echo "<li>No Data Found Macthing Your Query</li>";
}
?>
Here is the link where you can check it directly
http://speed-timing2.6te.net/
I believe your search query term is for example "event_name place_name" and in this case your query will not work. You can use FULLTEXT search instead.
For example
at first set fulltext index for eventname, place, country field as those are string fields.
$sql = "SELECT * FROM (
SELECT *, MATCH (eventname, place, country) AGAINST ('".$_POST["search"]."' IN BOOLEAN MODE) AS score
FROM events
ORDER BY score DESC
) AS temp
WHERE temp.score>0 OR temp.date LIKE '%".$_POST["search"]."%'
";
Please check manual here http://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html
I'm trying to follow this pagination tutorial http://www.phpfreaks.com/tutorial/basic-pagination to add pagination to my search page i've built. The following line from the tutorial:
$sql = "SELECT COUNT(*) FROM numbers";
I need it to count based on rows which match user inputted data that's being pulled in from a small form. This is the working query i was running before trying to add pagination
$raw_results = mysql_query("SELECT * FROM Pictures WHERE (`RimWidth` LIKE '%".$RimWidth."%') AND (`TyreWidth` LIKE '%".$TyreWidth."%') AND (`Aspect` LIKE '%".$Aspect."%') AND (`TyreDia` LIKE '%".$TyreDia."%') AND (`TyreMan` LIKE '%".$TyreMan."%')") or die(mysql_error());
I'm unsure how i can add this. I've tried this:
$sql = mysql_query("SELECT COUNT FROM Pictures WHERE (`RimWidth` LIKE '%".$RimWidth."%') AND (`TyreWidth` LIKE '%".$TyreWidth."%') AND (`Aspect` LIKE '%".$Aspect."%') AND (`TyreDia` LIKE '%".$TyreDia."%') AND (`TyreMan` LIKE '%".$TyreMan."%')") or die(mysql_error());
But it just errors out.
How do i need to format the query?
You are getting an error using COUNT instead of COUNT(expression), so you have to change your query so that you use SELECT COUNT(*) as in the first example you gave.
EDIT That's how you can get the exact count:
$query = "SELECT COUNT(*) FROM Pictures WHERE (`RimWidth` LIKE '%".$RimWidth."%') AND (`TyreWidth` LIKE '%".$TyreWidth."%') AND (`Aspect` LIKE '%".$Aspect."%') AND (`TyreDia` LIKE '%".$TyreDia."%') AND (`TyreMan` LIKE '%".$TyreMan."%')";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_fetch_row($result);
COUNT(*) - skipped what to count.
Basically if your query is working but you need to get the count instead of actual data you should be good with replacing the columns you're trying to select with the COUNT(*)
One more thing. The result of your last code line is not the string itself. It's the resource that can be used to fetch the count you need.
The following query take 10.86secs to initiate,
$sql="SELECT items.id i_id, status,manufacturerid,model,label,cpuno,corespercpu
from items,item2soft
where item2soft.itemid=items.id AND item2soft.softid={$r['id']}
order by label asc ";
While this code takes 23.73secs
$sql="SELECT items.id i_id, status,manufacturerid,model,label,cpuno,corespercpu
from items,item2soft
where item2soft.itemid=items.id AND item2soft.softid={$r['id']}";
The only difference between two codes is the latter has a ORDER BY keyword.Is there any way to make it faster.Please feel free to ask me anything.thanks for your help :)
After looking at your query - and by that I mean: adding proper indenting so I can actually read it - you probably just need to add some indexes.
$sql = "SELECT
items.id i_id,
status,
manufacturerid,
model,
label,
cpuno,
corespercpu
FROM
items,
item2soft
WHERE
item2soft.itemid = items.id
AND item2soft.softid = {$r['id']}
ORDER BY label ASC"
Add indexes on item2soft.itemid and item2soft.softid
If it's still slow, run an EXPLAIN
i have following sql query, everything works fine but when i put "and posted_date<>$datetime" its not retrieving data as per given command.
$datetime="0000-00-00";
$data = mysql_query("SELECT * FROM product_table where category_id=$cat1 or
pid=$par or gpid=$gpar and posted_date<>$datetime
ORDER BY autoid desc limit $no2,$cacount")
or die(mysql_error());
please check is that line is ok maybe i am doing mistake somewhere where category_id=$cat1 or pid=$par or gpid=$gpar and posted_date<>$datetime
maybe i need two where one for or and another for and...
Thanks
Try to group your condition and use DATE()
SELECT *
FROM product_table
where (category_id=$cat1 or
pid=$par or gpid=$gpar) AND DATE(posted_date) <> DATE($datetime)
ORDER BY autoid desc
LIMIT $no2, $cacount
My users can search for an order by an address right now. What I would like to do is let them be able to search with multiple criteria. Let them search by address, city, state, etc etc.
I have tried using the following code, but it doesn't seem to work.
$sql = ("SELECT order_number, sitestreet FROM `PropertyInfo` WHERE `sitestreet` LIKE '%$street%' OR `sitecity` LIKE '%$city%' AND `user` LIKE '$user'");
$result = mysql_query($sql);
I don't think it's reading the value in $user cause it displays all orders for all users.
How can I make it possible to search for an order using multiple serach values?
Thank you!
Wrap your OR statements in parenthesis so it forms one top-level condition, the user is the other top-level condition:
$sql = '
SELECT
`order_number`,
`sitestreet`
FROM
`PropertyInfo`
WHERE
(
`sitestreet` LIKE "%'.$street.'%" OR
`sitecity` LIKE "%'.$city.'%"
) AND
`user` = '.$user;
Also note, you want a direct match to the user column, use = instead of LIKE. I am assuming that $user is a numeric ID...
How about trying like
$sql = ("SELECT order_number, sitestreet FROM PropertyInfo WHERE (sitestreet LIKE
'%$street%' OR sitecity LIKE '%$city%') AND user LIKE '$user'");
AND has a higher order of precedence than OR (see http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html for details). You need to wrap your OR statement in parentheses so it get evaluated as one statement, before the AND statement.
SELECT order_number, sitestreet
FROM `PropertyInfo`
WHERE (`sitestreet` LIKE '%$street%' OR `sitecity` LIKE '%$city%')
AND `user` LIKE '$user'
Try:
$sql = ("
SELECT order_number, sitestreet
FROM `PropertyInfo`
WHERE (`sitestreet` LIKE '%$street%'
OR `sitecity` LIKE '%$city%')
AND `user` LIKE '$user'");
You should group the ORs together. The way it is written I believe it is reading it as 'if Street matches, ignore any other conditions (OR), otherwise both city and user must match.
Also G molvi's point is good, unless you're looking for a pattern match, go with =
HTH