This question already has answers here:
Passing an array to a query using a WHERE clause
(17 answers)
Closed 4 years ago.
I need help in issue related to MySQL database OR Operator, I am new to MySQL database so I am facing problems. Actually, I am building search engine filter.
I am getting two values from another page and show them on a search page by filtering from the database.
Here I declare values that come from another page
$cid = $_GET['cid'];
$plateform = $_GET['plateform'];
And here is my SQL
SELECT * FROM `products` WHERE (Cat_id = $cid) OR (plateform_id IN ($plateform)
when I try to get two values it gives me error like and when I get one value it works well. help me solve this problem.
It gives me an error , given below
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
you are missed close parenthesis at the end.
Need to change:
SELECT * FROM `products` WHERE (Cat_id = $cid) OR (plateform_id IN ($plateform)
To:
SELECT * FROM `products` WHERE (Cat_id = $cid) OR (plateform_id IN ($plateform))
Or you can also modify your query.
SELECT * FROM `products` WHERE Cat_id = $cid OR plateform_id IN ($plateform)
Related
This question already has answers here:
How to treat MAX() of an empty table as 0 instead of NULL
(3 answers)
SELECT max(x) is returning null; how can I make it return 0?
(7 answers)
Closed 1 year ago.
I am trying to insert different content based on if a SQL table has Rows. So far the Query will check what the max value of sortingOrder is and then + 1. However this query will break if there is no rows in the table. How can I implement a if statement to check if the table has no rows and then if it doesn't set the sortingOrder to '1'.
INSERT INTO faq (question, answer, sortingOrder)
VALUES ('$questionData', '$answerData', (SELECT MAX(sortingOrder) FROM faq C) +1)
Thanks
The best solution is to make sortingOrder an AUTO_INCREMENT column. The database will assign the values automatically, incrementing them for each row.
If you can't do that for some reason, you can check if the subquery returns NULL and replace it with 1.
INSERT INTO faq (question, answer, sortingOrder)
SELECT '$questionData', '$answerData', IFNULL(MAX(sortingOrder)+1, 1)
FROM faq
This question already has answers here:
How do I check if a column is empty or null in MySQL?
(21 answers)
Closed 3 years ago.
I am creating a table where I want to show the data where one column is empty.
I tried everything but nothing seems to work
$sql = "SELECT * from tblleaves where empid=:eid and EndKm is NULL order by PostingDate desc";
In other words, I want to fetch data where endkm column is empty or has spaces only.
If I remove EndKm is NULL everything works fine for me.
Try SELECT * from tblleaves where empid=:eid and (EndKm IS NULL or TRIM(EndKm) = '') order by PostingDate desc
This question already has answers here:
How do I select all the columns from a table, plus additional columns like ROWNUM?
(4 answers)
Closed 5 years ago.
Some SQL toolsets like PDO can do special things based on the first column selected, such as take it as class name to instantiate or to use it as key into a hashtable. Unfortunately PDO removes that column from the result. What if I still want it to be part of the result?
I've tried queries such as
SELECT `class` as `myclass`, * FROM `mytable`
but I'm getting errors:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM mytable at line 1
I understand that there can't be conflicts in column names, hence the
as `myclass`
And the following works just fine:
SELECT `class` as `myclass`, `class` FROM `mytable`
Is this possible at all without doing a self-join or putting the full list of columns?
You can do like below
SELECT `mytable`.`class` as `myclass`, `mytable`.* FROM `mytable`
or like this
SELECT t.class AS myclass, t.* FROM mytable t;
This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 7 years ago.
My code, which archives old members from a membership database, when run, returns
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELETE FROM members WHERE member_ref = 155' at line 5.
It should copy the entry to the identical 'archive' table from 'members' and delete the original in members.
$ref = '155';
$leave = mysql_query(" INSERT INTO archive
SELECT *
FROM members
WHERE member_ref = ".$ref.";
DELETE FROM members
WHERE member_ref = ".$ref ) or die(mysql_error());
I know I should be using mysqli but I haven't had time to standardise the whole system (~120 pages) from mysql to mysqli.
It's parsing 155 into the second $ref
Using the phpmyadmin's SQL window and entering the following works perfectly.
INSERT INTO archive
SELECT *
FROM members
WHERE member_ref = 155;
DELETE FROM members
WHERE member_ref = 155
What am I missing?
Use Separate Call, like this, See Details About mysql_query
$leave = mysql_query(" INSERT INTO archive
SELECT *
FROM members
WHERE member_ref = '$ref' ") or die(mysql_error());
$leave = mysql_query(" DELETE FROM members
WHERE member_ref = '$ref'") or die(mysql_error());
If you go to the PHP Manual, it is written right on the first line:
mysql_query() sends a unique query (multiple queries are not supported)
http://php.net/manual/en/function.mysql-query.php
Therefore you should put your queries in separate mysql_query() calls.
This question already has answers here:
How can I search within a table of comma-separated values?
(6 answers)
Closed 8 years ago.
For example, when a table has a record column named 'product' that contain value such as: 'Laptop, Desktop, Case'. How can I validate these 3 values that break down with a comma against two PHP variables value with $var1='Laptop' and $var2='Desktop' ? So that this row can be found! However, the two variables could be passed in the order of 'Desktop', 'Laptop' as well. Meanwhile, the column could have pattern of 'Case, Desktop, Laptop'. I wonder if there is a solution in MySQL for this kind of scenario that somehow, pick up each element like PHP could and match them with each var individually.
Without knowing anything about your table structure this is a quick example of what you can do.
SELECT * FROM table WHERE $var1 IN (SELECT product FROM table WHERE something = somethingelse) AND $var2 IN (SELECT product FROM table WHERE something = somethingelse)
As I understood, you want the data to be found, if the column 'product' contains 'Laptop' or 'Desktop'. Write this with the LIKE operator in your query:
"SELECT * FROM table WHERE `product` LIKE '%Desktop%' OR `product` LIKE '%Laptop%'"
If you pass the variables it would be:
"SELECT * FROM table WHERE `product` LIKE '%$var1%' OR `product` LIKE '%$var2%'"
Make sure to use the % sign before and after the searched string, so that it will match even if the keyword is anwhere inside the product content.