EDIT: Strange I vardumpped every variable they are all a bool(false)
I'm trying to make this query. But when I click the search button nothing shows up.
Someone who can direct me in the right direction? Thanks in advance.
$STH = $DBH->query("SELECT * FROM artikel
LEFT JOIN barcode
ON artikel.barcode_id = barcode.barcode_id
LEFT JOIN debiteur
ON artikel.debiteur_id = debiteur.debiteur_id
WHERE debiteur.debiteur_naam LIKE '%$formulier%'
AND artikel.artikel_leverancier LIKE '%$leverancier%'
AND artikel.artikel_leverancier_code LIKE '%$artikelleverancier%'
AND artikel.artikel_code LIKE '%$artikelexact%'
AND artikel.artikel_omschrijving LIKE '%$artikelomschrijving%'
ORDER BY '$orderby' ASC");
Assuming you are interested in items which satisfy atleast one LIKE condition, you need to replace ANDs with ORs
$STH = $DBH->query("SELECT * FROM artikel
LEFT JOIN barcode
ON artikel.barcode_id = barcode.barcode_id
LEFT JOIN debiteur
ON artikel.debiteur_id = debiteur.debiteur_id
WHERE debiteur.debiteur_naam LIKE '%$formulier%'
OR artikel.artikel_leverancier LIKE '%$leverancier%'
OR artikel.artikel_leverancier_code LIKE '%$artikelleverancier%'
OR artikel.artikel_code LIKE '%$artikelexact%'
OR artikel.artikel_omschrijving LIKE '%$artikelomschrijving%'
ORDER BY '$orderby' ASC");
It sounds like you have to test for empty parameters. Here is one way in SQL:
WHERE ('$formulier' <> '' and debiteur.debiteur_naam LIKE '%$formulier%') or
('$leverancier' <> '' and artikel.artikel_leverancier LIKE '%$leverancier%') or
('$artikelleverancier' <> '' and artikel.artikel_leverancier_code LIKE '%$artikelleverancier%') or
('$artikelexact' <> '' and artikel.artikel_code LIKE '%$artikelexact%') or
('artikelomschrijving' <> '' and artikel.artikel_omschrijving LIKE '%$artikelomschrijving%')
Alternatively, you can build up the query clause by clause when the variables are not empty.
Related
i have some struggle to search table.
here the example
SELECT * FROM table_tutup_kas
LEFT JOIN table_formulir_pelita
ON table_formulir_pelita.id_formulir=table_tutup_kas.id_formulir
WHERE nomor_formulir LIKE '%mikha%' or harga_pelita LIKE '%5%' and deleted=0
the result still showing data with status deleted=1.
any other method for fix my query?
The AND logical operator has a higher precedence than OR . Try using parenthesis :
SELECT * FROM table_tutup_kas
LEFT JOIN table_formulir_pelita
ON table_formulir_pelita.id_formulir=table_tutup_kas.id_formulir
WHERE (nomor_formulir LIKE '%mikha%' or harga_pelita LIKE '%5%' ) and deleted=0 ;
Have two SQLite tables:
sh_item
id
title
..
and
sh_paramvalues
id
paramid
itemid
value
Have some query that I need to modify
//$get[0] - search query from user
$this->q = "SELECT *
FROM sh_item
WHERE title LIKE '%".$get[0]."%'
OR descr LIKE '%".$get[0]."%'
LIMIT '".$this->page->start."','".$this->page->on1page."'";
I need some query that could get value ( LIKE '%".$get[0]."%') from sh_paramvalues where paramid='some my value' and itemid = id which use sh_item in base query.
Maybe I need to use join but I am not good at that.
Try:
//$get[0] - search query from user
$this->q = "SELECT *
FROM sh_item si
INNER JOIN sh_paramvalues sp
ON si.id = sp.itemid
WHERE (si.title LIKE '%".$get[0]."%'
OR si.descr LIKE '%".$get[0]."%'
OR sp.value LIKE '%".$get[0]."%')
AND sp.paramid = 3
LIMIT '".$this->page->start."','".$this->page->on1page."'";
I'm building a search function for my site, however the MySQl query won't read the PHP variables, and I don't mean errors, it just seems to think they're NULL.
My current code is:
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('library', $conn);
$sql = "SELECT * FROM Books";
if($_POST['find']!="")
{
if($_POST['field'] == "Books")
{
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE books.BookName LIKE '%''".($_POST['find'])."''%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
}
else if ($_POST['field'] == "Authors")
{
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE authors.Forename LIKE '%J.%'
AND authors.Surname LIKE '%%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
}
}
$result = mysql_query($sql, $conn) or die("Can't run query");
$loopnumber = 1;
if (mysql_num_rows($result) ==0 ){echo "No Results have been found";}
The POST variable does contain data as I've tested by echo'ing it, however my site just gives the "No Results have been found" message meaning the query retuned no results.
Even if I pass the POST into a normal variable I get the same results.
However if I remove the "LIKE '%%'" and have it look for and exact match from typing in the search on the site it works fine.
Edit: Hmmmm, just made it so I pass the POST into a variable like so..
$searchf = "%".$_POST['find']."%";
and having that variable in the WHERE LIKE makes it work, now I'm just curious as to why it doesn't work the other way.
I seems to love quotation marks too much, and should go to bed.
Well first of all, I am guessing you are getting a MySQL syntax error when trying to execute that first query. This line:
WHERE books.BookName LIKE '%''".($_POST['find'])."''%'
Should be
WHERE books.BookName LIKE '%".$_POST['find']."%'
Because right now you are getting
WHERE books.BookName LIKE '%''ABC''%'
when you should be getting
WHERE books.BookName LIKE '%ABC%'
I don't admit to understand what you are doing with your second query, which just hard codes and has %% as one of the search criteria, which is, in essence meaningless.
Its in your LIKE expression. If in $_POST['find'] the value is LOTR the query would be
WHERE books.BookName LIKE '%''LOTR''%'
and the resault would be empty. Just remove the double ' and it should be work.
Try this way:
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE books.BookName LIKE '%".$_POST['find']."%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
It should be work
use this, worked for me:
$query_casenumber = "SELECT * FROM pv_metrics WHERE casenumber='$keyword' OR age='$keyword' OR product='$keyword' OR eventpreferredterm='$keyword' OR patientoutcome='$keyword' OR eventsystemclassSOC='$keyword' OR asdeterminedlistedness='$keyword' OR narrative LIKE '%".$_POST['keyword']."%' ";
The title is a bit confusing, hopefully it is clear what I am trying to do from my query. I thought I understood joins, clearly not, here is my $query:
SELECT DATE(T0.timestamp),
SUM(T0.total_responses),
SUM(T0.responses),
T0.metric_id,
T1.metric_id
FROM `personal_aggregates` AS T0
LEFT JOIN `qrs_metrics` AS T1
ON T0.metric_id = T1.qrs_metric_id
WHERE T0.user_id = 1 AND
T0.duration = '1' AND
T0.category_id IN (1,2,3,4) AND
T0.timestamp >= 'period_duration'
GROUP BY DATE(T0.timestamp)";
I am trying to join the tables on the columns metric_id and qrs_metric_id. However I get no results from the above query. When I loop through the result in PHP and check any of the variables, for example $result['T0.metric_id'], I get Undefined index: T0.metric_id
If anyone can shed any light on this I would much appreciate it.
Try this:
$query = "SELECT DATE(T0.timestamp), SUM(T0.total_responses), SUM(T0.responses),
T0.metric_id AS metric0, T1.metric_id AS metric1
FROM `personal_aggregates` AS T0
LEFT JOIN `qrs_metrics` AS T1 ON T0.metric_id = T1.qrs_metric_id
WHERE T0.user_id = 1 AND T0.duration = '1'
AND T0.category_id IN (1,2,3,4)
AND T0.timestamp >= 'period_duration'
GROUP BY DATE(T0.timestamp)";
you can now make a difference by using this:
echo $row['metric0'];
echo $row['metric1'];
Either you need to add some aliases to your selected columns
SELECT DATE(T0.timestamp) as tstamp, SUM(T0.total_responses) as responses...
and refer to the columns by alias:
$result['tstamp']
or you can refer to the columns by numeric index:
$result[0]
Your query should be
$query = "SELECT
DATE(T0.timestamp) AS T0timestamp,
SUM(T0.total_responses) AS total_responses,
SUM(T0.responses) AS responses,
T0.metric_id,
T1.metric_id AS metric_id_2
FROM `personal_aggregates` AS T0
And accessed like
$result['metric_id'];
$result['metric_id_2'];
$result['T0timestamp'];
$result['total_responses'];
Exclude the table alias from the associative array access e.g. "T0.XXX" is wrong. You can only do that in the SQL
Hey, I think I'm going about this the wrong way, but have tried many ways, none of which give me the desired results.
Basically I have one search field which is to check multiple tables. My issue is that when, say, an item doesn't have a related person, it won't return a result for that item. Other than that it is fine. I realize the problem is in the WHERE, demanding that it find only records that match in the other table, but I'm not sure how to rectify this.
Thanks!
$q = "SELECT DISTINCT item.*
FROM item, item_people, people
WHERE item.record_id = item_people.item_id AND people.record_id = item_people.people_id
AND
item.live = '1'
AND
(concat_ws(' ',people.name_first,people.name) LIKE '%$search_param%' OR
item.name_title LIKE '%$search_param%' OR
item.city = '$search_param' OR
item.category LIKE '%$search_param%' OR
item.on_lists LIKE '%$search_param%')
$limit";
You would need an OUTER JOIN to return items without related people.
SELECT DISTINCT item.* /*But don't use *!
*/
FROM item
LEFT OUTER JOIN item_people
ON item.record_id = item_people.item_id
LEFT OUTER JOIN people
ON people.record_id = item_people.people_id
WHERE item.live = '1'
AND
(
concat_ws(' ',people.name_first,people.name) LIKE '%$search_param%'
OR item.name_title LIKE '%$search_param%'
OR item.city = '$search_param'
OR item.category LIKE '%$search_param%'
OR item.on_lists LIKE '%$search_param%'
)
$limit