PHP MySQL While Loop Multiple tables - php

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)

Related

PHP Fetch data with two different function

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/

PHP + MySQL 3 Condition

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;
";

array of like query for all fields

I have an array like array('first','12'). So i need to select the table using like condition. which fields have those values.
Example Query:
SELECT * FROM `class` WHERE name LIKE '%first%'
OR father_name LIKE '%first%'
OR age LIKE '%first%'
OR name LIKE '%12%'
OR father_name LIKE '%12%'
OR age LIKE '%12%'
I need to find all the fields with or condition. Is this good or have any other options to find an array of values in all fields.
Thanks,
You can shorten the query using REGEXP, like this:
SELECT * FROM class
WHERE name REGEXP 'first|12'
OR father_name REGEXP 'first|12'
OR age REGEXP 'first|12';
Here's the reference:
REGEXP
And like you said, if you have an array like this,
$arr = array('first',12);
Then your query should be like this:
$query = "SELECT * FROM class WHERE name REGEXP '" . $arr[0] . "|" . $arr[1] . "' OR father_name REGEXP '" . $arr[0] . "|" . $arr[1] . "' OR age REGEXP '" . $arr[0] . "|" . $arr[1] . "'";

Creating a search engine

I'm trying to create a search engine to search for users based on their name.
Here is an example of what I can do at this moment:
I want to search for david (surname) Jones (name)
-> When I type: 'Da' or 'vid' or 'J' or 'ones' it gives me David Jones
BUT when I type 'David J' it gives nothing.
In which way do I have to change my query to search on both fields on the same time?
The query:
public function Search($searchinput)
{
$db = new Db();
$select = "SELECT * FROM tblusers WHERE name LIKE '%" . $searchinput . "%' OR surname LIKE'%" . $searchinput . "%'";
$result = $db->conn->query($select);
return $result;
}
probably for production you can use http://sphinxsearch.com/ because it is MUCH faster than MySQL in searching texts in natural language
Try this query using CONCAT:
$select = "SELECT * FROM tblusers WHERE name LIKE '%". $searchinput ."%' OR surname LIKE '%". $searchinput ."%' OR CONCAT(name,' ',surname) LIKE '%". $searchinput ."%' ";
The problem is that you are trying to match David J in name or surname but as I see your db look as like follows:
Name | Surnam
Davud | Jones
And your like statement is looking for: "David J" into name or surname which give you the right result(zero) because there does not exist any row with name or surname like "David J", in your case the best solution will be to concat the name with surname and then to make like statment upon them:
Select * FROM tbulsers WHERE CONCAT(name, ' ', surname) LIKE "%David J%"

Return row if non existent in other table

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.

Categories