-2

I want to search multiple values from database field.

below is my query.

For example

_$Shape = Makeable2,Clivage Brn_ ;
_$Color = GHI,JKL_

SELECT * FROM inventory WHERE Shape IN ($Shape) OR Color IN ($Color)

enter image description here

coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
  • 2
    Not only are you wide open to SQL injections and should use paramerized prepared statements, but you also need to actually give us a description of your problem. What happens? What's the expected results and so on. You should also show us _all_ the relevant code. Not just bits and pieces. – M. Eriksson Jan 19 '19 at 10:42
  • i'm searching data with MySQL database, so search not properly i'm tried – Dharmendra Kanpara Jan 19 '19 at 10:44
  • 1
    That didn't clarified anything. Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – M. Eriksson Jan 19 '19 at 10:53
  • Don't put multiple values into a single scalar variable. Use an array if the values are related or use different variables. Once you solve that, do more research on prepared statements and binding parameters using IN. – Devon Bessemer Jan 21 '19 at 12:02
  • 1
    And what is not working with the given query? – Nico Haase Jan 21 '19 at 13:18

1 Answers1

0

I wrote my answer, and I see you now edited the post, still going to post what I wrote.

Not sure what it is you want to do exactly. But here an example of what I think it is you wish to achieve. Assuming that all values in your array are sanitized... This query would work. As said before by others, you need to create an array with all the values you would like to search for. Adjust to suit your needs.

$ArrayA = array("round", "circle", "something");
$ArrayB = array("red" , "green");

$sql = "SELECT * FROM inventory WHERE Shape IN ('".implode("','",$ArrayA)."') OR Color IN ('".implode("','",$ArrayB)."')";

Database layout example;

id  Shape      Color
1   round      red
2   round      blue
3   square     red
4   square     green
5   circle     blue
6   circle     red
7   circle     green
8   something  blue
9   something  green
Paddy
  • 123
  • 7