I have a question for this, maybe I am doing the wrong way
as I have created a two different search function which they have own different function search
as one is SKU and one is Date
I have created a code for MySQL and Workbench and it working fine as I'm using
SELECT
settlement_id, sku
FROM
settlements
WHERE
sku LIKE 'ISCE%'
AND settlement_id LIKE '7072432852'
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
and it displayed fine
I use this same function for the PHP pages
$output = '';
if (isset($_POST["query"])) {
$search = mysqli_real_escape_string($conn, $_POST["query"]);
$queryskulist = "
SELECT
settlement_id,
sku,
FROM settlements
WHERE sku LIKE '%" . $search . "%' and settlement_id LIKE '%" . $search . "%'
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
";
} else {
$queryskulist = "
SELECT
settlement_id,
sku,
FROM settlements
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku ";
}
as you can see
WHERE sku LIKE '%" . $search . "%' and settlement_id LIKE '%" . $search . "%'
that's where I am getting an error as I am able to get data from only one function like this
WHERE sku LIKE '%" . $search . "%'
but I am unable to get two different search result, should I use two different query $_POST["query"]?
Any advice would be great, or Sample/Example demo PHP site would be great, it would help me a big picture on it.
You can fix this by using the following
SELECT
settlement_id,
sku
FROM
settlements
WHERE
(CONCAT(sku, settlement_id) LIKE '%" . $search . "%')
AND
sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY
sku
Alternatively you should be able to wrap in parenthesis
WHERE (sku LIKE '%" . $search . "%') OR (settlement_id LIKE '%" . $search . "%')
do the fact you have not aggregation function you should use where
SELECT
settlement_id
, sku
FROM settlements
WHERE sku LIKE 'ISCE%'
AND settlement_id LIKE '7072432852'
WHERE sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
the use of group by without aggreagtion function is deprecated in SQL .. and in the most recent version of mysql is not allowed .. (by default)
using group by without aggregation function you could obtain unpredictable result
Thank for the update, I have fixed this issue as I have decided to remove 'Sku' and keep the rest of it, so here is what happened.
'Settlement_id' is used an options dropdown value on the PHP pages which they are pulling data from MySQL and the 'SKU' search box are using on the input box. and the input box is using Javascript Ajax Filters to get data which it displayed result from 'Settlement_Id' data
here is a demo
http://amazonsettlement.ishka.ie/MultiSelect/
Related
I am creating an advanced search feature for a website and it's almost done, I'm only having one major issue.
I am matching the rooms like this:
AND Rooms=" .$_SESSION["room"] ."
and tried this as well:
AND (Rooms=" .$_SESSION["room"] ." OR Rooms IS NULL)
But the problem is if the user doesn't insert any value in the room input it won't show any room. And with the IS NULL code if I insert "8" in the rooms input if there is no matches it will display all values from the DB.
I don't want to make the input as required.
I just need a solution with mysql for when the field is empty return all values without using this:
if ($_SESSION["room"]==NULL) {}
else{}
Full query:
`SELECT * FROM secret WHERE secretIDName='1' AND NatureTypeIDName LIKE '%" .$_SESSION["nature"] ."%' AND (NettArea>="
.$_SESSION["NettArea"] ." OR NettArea IS NULL) AND ConditionTypeIDName LIKE'%" .$_SESSION["lifestyle"]
."%' AND ((SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1)>="
.$_SESSION["BusinessTypeValuesMin"]
." AND SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1)<="
.$_SESSION["BusinessTypeValuesMax"]
.") OR SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1) = '') AND (SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',2),'|',-1)='"
.$_SESSION["BusinessTypeValuesType"]
."' OR SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',2),'|',-1)='') AND GarageArea>="
.$_SESSION["GarageArea"]
." AND (LocationIDName LIKE '%"
.$_SESSION["zone1"]
."%' AND LocationIDName LIKE '%"
.$_SESSION["zone2"]
."%' AND LocationIDName LIKE '%"
.$_SESSION["zone3"]
."%') AND (Rooms="
.$_SESSION["room"]
.") LIMIT "
.($page-1)*$Page
.", " .$Page ."";`
You can create the condition like this (I assume, that the "rooms" is number representing number of rooms?):
AND (Rooms = ".(int)$_SESSION['room']." OR ".(int)$_SESSION['room']." = 0)
If $_SESSION['room'] is empty (user haven't specified number of rooms), You get
AND (Rooms = 0 OR 0 = 0)
... which is always TRUE, so the "rooms" condition doesn't apply at all. If user specified number of rooms, the query would look like:
AND (Rooms = 8 OR 8 = 0)
The 8 = 0 is always FALSE, so effectively, You have the condition You need: Rooms = 8.
In your query, checking NULL isn't the same as checking empty. I'd recommend the following:
AND (Rooms=" .$_SESSION["room"] ." OR Rooms IS NULL OR Rooms <>'')
Also, it's highly recommended filtering the $_SESSION variable before injecting that into MySQL, if it's a number, assign it to $room=(int)$_SESSION['room'] to force it to be an integer.
There are several solutions, one of them is simply to store the SQL in a string variable, and then add the rooms conditions if the value is not null.
$sql = SELECT ...
if ($_SESSION["room"] !== NULL) {
$sql = $sql . ' AND Rooms=".$_SESSION["room"] . " '
}
Try the query like this: Note i concatenate all php varibles with {} instead of ". ."
SELECT * FROM secret WHERE secretIDName='1' AND NatureTypeIDName LIKE '%{$_SESSION["nature"]}%' AND (NettArea >={$_SESSION["NettArea"]} OR NettArea IS NULL) AND ConditionTypeIDName LIKE'%{$_SESSION["lifestyle"]}%' AND ((SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1)>={$_SESSION["BusinessTypeValuesMin"]} AND SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1)<={$_SESSION["BusinessTypeValuesMax"]}) OR SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',4),'|',-1) = '') AND (SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',2),'|',-1)='{$_SESSION["BusinessTypeValuesType"]}' OR SUBSTRING_INDEX(SUBSTRING_INDEX(BusinessTypeValues,'|',2),'|',-1)='') AND GarageArea>={$_SESSION["GarageArea"]} AND (LocationIDName LIKE '%{$_SESSION["zone1"]}%' AND LocationIDName LIKE '%{$_SESSION["zone2"]}%' AND LocationIDName LIKE '%{$_SESSION["zone3"]}%') AND (Rooms={$_SESSION["room"]}) LIMIT ($page-1)*$Page, $Page";
so I have this query below in my php code :
$query ="SELECT *
FROM material_tools_master_data
WHERE material_name like '" . $_POST["keyword"] . "%'
ORDER BY material_code
LIMIT 0,50";
It does pretty well and give me a result called 'autocomplete' in my form. The problem is, I wanna make it more complex, I want my autocomplete filter the data selection not only by material_name but also with material_tools_group and show me exactly the material_name which is filtered by material_group = 'Measuring' OR 'Tools'.
The point is, I want to make this query works with my autocomplete. So here is my new query :
$query ="SELECT *
FROM material_tools_master_data
WHERE `material_tools_group` = 'Measuring' OR 'Tools' AND `material_name` like '" . $_POST["keyword"] . "%'
ORDER BY material_code LIMIT 0,50";
The query above is not working, the query above is giving me all the material_name rows in the table.
Any help will be much appreciated.
See warnings about PHP's deprecated API, and the proper use of prepared statements above...
$query ="
SELECT *
FROM material_tools_master_data
WHERE material_tools_group IN('Measuring','Tools')
AND `material_name` LIKE '" . $_POST["keyword"] . "%'
ORDER
BY material_code LIMIT 0,50;
";
Morning peps!
I'm having some troubles getting my query to work properly. I've created a search field which pulls rows from multiple tables (invoices, customers). For example, when ever I'm searching for invoice "2014-003", which is the only invoice with that nr., then I'm getting a list of all of my customers and then the same invoice nr. all the way down..
My code:
$results = $db->query("SELECT * FROM invoices, customers
WHERE invoice.customer_id = customers.id AND
(customers.name LIKE '%" . $search . "%') OR
(invoices.invoice_nr LIKE '%" . $search . "%') OR
(invoices.email LIKE '%" . $search . "%') OR
(customers.email LIKE '%" . $search . "%')");
I suck at english, so I think it's better for me to maybe write an example below:
I'll search for the invoice nr. "2014-003" which is unique, and what I get is this:
Peter - 2014-003
Christian - 2014-003
Adam - 2014-003
Frederick - 2014-003
Peter - 2014-003
and it should return:
Adam - 2014-003
because Adam is the only one who has an invoice with the following invoice nr.
I hope you'll understand what I want to do :)
Give this a whirl:
$results = $db->query("
SELECT
*
FROM
invoices,
customers
WHERE
invoice.customer_id = customers.id AND
((customers.name LIKE '%" . $search . "%') OR
(invoices.invoice_nr LIKE '%" . $search . "%') OR
(invoices.email LIKE '%" . $search . "%') OR
(customers.email LIKE '%" . $search . "%')
)");
Your ORs are not grouped properly, hence the AND validates, but is later relaxed because it gets a hit on one of the ORs - whereas your OR's are really referring to a group of possible conditions (but the original join needs to stay in place)
I have 2 tables
members
=======
id
f_name // get these values.
l_name
email
friends
=======
to
from
The users's I'd is the value $member_id, if it is present in "to" or "from" I want it to not return the other value of that row, so only non-friends are shown.
Im creating a page to allow members to search through the database to add their friends by email, or name.
I'd like to return all rows from members where there is no record of the userid in either the "to" or "from" columns of the friends table.
I know how to do most basic mysql but joins are an issue for me, i don't really understand how to write them, if anyone can help me out and maybe even explain it a bit that would be great!
My query now:
$query = "SELECT * FROM members WHERE f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10";
//Where $word is the search term.
Modified Query
SELECT * FROM members WHERE id NOT IN (SELECT to FROM friends WHERE frm=$member_id) AND $member_id NOT IN (SELECT frm FROM friends WHERE to=$member_id) AND f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10
Edited
So, suppose a user with id 3:
SELECT *
FROM members
WHERE id <> 3
AND id NOT IN (SELECT to FROM friends WHERE from = 3)
AND id NOT IN (SELECT from FROM friends WHERE to = 3)
[Other conditions....];
OR
SELECT *
FROM members
LEFT JOIN friends
ON (members.id = friends.to
OR members.id = friends.from)
AND (members.to = 3
OR members.from = 3)
WHERE friends.to IS NULL
AND id <> 3
[Other conditions....];
Maybe consider change the name of your "from" and "to" columns, to avoid confusion, because FROM and TO are reserved words.
Also, you can use the reserved word EXPLAIN , before the query, to see the difference of the number of rows being fetched.
You might be able to do that using an LEFT Join, which will return all rows from members even if there's no corresponding row on the friend's table.
Something like this:
SELECT a.*, f.* FROM members m LEFT JOIN friends f
A great article to understand the many joins there are is this one.
any ideas how to get orderTotal and orderId inside the tpl_checkout_success_default for the conversions tracking purposes ?
So far it looks like order id can be accessed by using this variable $zv_orders_id but how to get order total ?
will this code work:
$orders_query = "SELECT * FROM zen_orders WHERE orders_id = " . $zv_orders_id ." LIMIT 1";
$orders = $db->Execute($orders_query);
$order_total = $orders->fields['order_total'];
many thanks,
cheers
look in /includes/modules/pages/checkout_success/header_php.php
in there you will see the queries already being run by zencart to do with your order, and id say its already pulling out the info you want.
so you just need to set said data you need to a variable that you can then use in your tpl_checkout_success_default.php file.
eg, something like $customer_has_gv_balance, you will see where it is set in the hearder file and then used in the template file
heres something i found in order.php that would almost do it as is:
$order_total_query = "select text, value
from " . TABLE_ORDERS_TOTAL . "
where orders_id = '" . (int)$order_id . "'
and class = 'ot_total'";
$order_total = $db->Execute($order_total_query);
For a simple tracking code like one used for a shopping comparison site, I've used the following for the order ID and order amount. Use these in the tpl_checkout_success.php page
Order ID:
echo $zv_orders_id;
Use this select statement:
$to_send_sql = 'select REPLACE (text,"$","") text from orders_total where orders_id = '.$zv_orders_id.' and class = "ot_subtotal"';
$to_send= $db->Execute($to_send_sql);
Order amount:
echo $to_send->fields['text'];
Hope this helps someone!