-1

I am trying to compare a variable that contains a string of fruits and is stored in a mysql database to an array of fruits. My code looks like this:

$stored_fruits = mysqli_query($this->con, "SELECT fruits FROM fruit_table"); // uploaded fruits: "Apple Orange Pineapple"
$fruit_array = explode(", ", $fruits); // fruits are searched for and turned into an array: ["Apple", "Strawberry", "Banana"]
for($i = 0; $i < count($fruit_array); $i++) {
    if($fruit_array[$i] == substr($stored_fruits)) {
        echo "fruit match";
    }
}

$fruits is a string of fruits that is taken from what the user types in and $stored_fruits accesses strings of fruits already in the database. $fruits is an array that looks like ["Apple", "Pineapple", "Strawberry"]. $stored_fruits looks like: "Strawberry Orange Banana". I want to compare each element in the array to the uploaded fruits. If there is a match, I want it to echo match. Any ideas? Thanks.

evanb629
  • 1
  • 5
  • `mysqli_query()` doesn't return the results of the query, you need to call `mysqli_fetch_assoc($stored_fruits)`. And you need to call it in a loop to get all the results. – Barmar Mar 27 '18 at 18:41
  • There's no way that `$stored_fruits` can hold that string. – Barmar Mar 27 '18 at 18:42
  • `substr()` needs at least 2 arguments: the string, a starting position, and an optional length. I think you really want to use `strpos()` to see if the string contains a substring. – Barmar Mar 27 '18 at 18:43
  • Use `explode()` to turn the space-separated string into an array, then use `array_intersect()` to determine if the two arrays share any elements. – Alex Howansky Mar 27 '18 at 18:44
  • You seem to have very little understanding of any of the functions you're calling. – Barmar Mar 27 '18 at 18:44
  • 1
    Does `fruit_table` really have just one row containing a space-separated list of fruits? – Barmar Mar 27 '18 at 18:44
  • Not sure what you intended with `$this->,` in the first line. – Nigel Ren Mar 27 '18 at 18:45
  • The string is in one column, called "fruits", so I just used "SELECT fruits FROM fruit_table" because I thought that holds the value of the column. I am not that experienced in PHP so there is a good chance I am wrong. Thank you though. – evanb629 Mar 27 '18 at 18:45
  • @NigelRen `$this->connection` or something like that probably got lost in translation – Don't Panic Mar 27 '18 at 18:46
  • fruit_table has two columns, one being an ID, the other being the string of fruits – evanb629 Mar 27 '18 at 18:46
  • $this->con is working and has been working for other parts in my project so I kept it the same – evanb629 Mar 27 '18 at 18:47
  • 1
    @evanb629 But you wrote `$this->` instead of `$this->con`. – Barmar Mar 27 '18 at 18:48
  • Check this out: https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad. Just some food for thought. – Don't Panic Mar 27 '18 at 18:50
  • @Barmar i meant to write $this->con, where $con is my connection variable . . . my bad – evanb629 Mar 27 '18 at 18:50
  • 1
    So why don't you just fix it? – Barmar Mar 27 '18 at 18:51
  • @Don'tPanic He's not storing a delimited list in the table. Each fruit is in a different row. He assumed that `mysqli_query()` would concatenate them. – Barmar Mar 27 '18 at 18:52
  • @Barmar are you sure? Maybe I misinterpreted this comment: https://stackoverflow.com/questions/49520217/compare-an-array-of-strings-to-a-string-stored-in-a-databse#comment86046245_49520217 – Don't Panic Mar 27 '18 at 18:52
  • No, @Don't Panic is right. I am storing the string of fruits in one row. So one row could look like "Apple Banana Orange". – evanb629 Mar 27 '18 at 18:53
  • 1
    That's a bad design. And if you only have one row, why bother with an `ID` column? – Barmar Mar 27 '18 at 18:54
  • I guess you're right. I just thought that since each row in the table is different, it would make sense to put in an auto-incrementing ID along with the string of fruits. – evanb629 Mar 27 '18 at 18:57

1 Answers1

0

You need to fetch the row containing the result of the query. Then you can use explode() to split the fruits using space as the delimiter, and compare the two arrays to see if they have any fruits in common.

$results = mysqli_query($this->con, "SELECT fruits FROM fruit_table");
$row = mysql_fetch_assoc($results);
$stored_fruits = explode(" ", $row['fruits']);

$fruit_array = explode(", ", $fruits);
if (!empty(array_intersect($stored_fruits, $fruit_array))) {
    echo "fruit match";
}

But it would be better to have each fruit in a separate row of the table. Then you can fetch all the fruits with a loop:

$results = mysqli_query($this->con, "SELECT fruit FROM fruit_table");
$stored_fruits = array();
while ($row = mysql_fetch_assoc($results)) {
    $stored_fruits[] = $row['fruit'];
}
Barmar
  • 741,623
  • 53
  • 500
  • 612