1

Hi I am trying to use in_array, I think my syntax is correct, but it says "Wrong datatype for second argument"

My code is

$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");

while($row = mysqli_fetch_array($result))
    {
    if (in_array($row['ProductID'], $filter))
        {
        }
    }

My idea is to find out if the ProductID from Products Table is in the Order Table.

Could someone helps me, Thanks :-)

2 Answers 2

3

$filter isn't an array; it's a mysqli_result object:

$filter = mysqli_query($con, "SELECT ProductID FROM Orders");

I think you want to iterate over that, add each ProductID to a new array, and then pass that array to the in_array function like so:

$filter = mysqli_query($con, "SELECT ProductID FROM Orders");

$product_ids = array();

while ($row = $filter->fetch_assoc())
{
    $product_ids[] = $row['ProductID'];
}

$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");

while($row = mysqli_fetch_array($result))
{

    if (in_array($row['ProductID'], $product_ids))
    {

    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks for helping, but is seems does not work. The table does not display anything inside...
You are putting this outside (before) the while($row = mysqli_fetch_array($result)), right?
no, do you mean put these two loops before the while($row = mysqli_fetch_array($result)), and then I am going to have two while loops, and the second one is inside the if loop? Sorry I am not quite fimilar with PHP...
oh thanks so much, it solved. Actually the answer shouldn't use "$row" twice, and I correct it. Anyway, THanks for helping :-)
3

Your code is failing because $filter is a MySQLi result resource, not an array. Really, this is better accomplished with a simple inner join between the two tables. If a ProductID does not exist in Orders, the INNER JOIN will exclude it from the result set in the first place.

$sql = "
SELECT Products.* 
FROM
  Products
  INNER JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";

$result = mysqli_query($con, $sql);
if ($result) {
  $results = array();
  while ($row = mysqli_fetch_array($result)) {
    $results[] = $row;
  }
}
// Now $results is a 2D array of all your Products

If instead you want to retrieve all the Products, and simply have an indication of whether it has an active order, use a LEFT JOIN and test if Orders.ProductID is null in the SELECT list:

$sql = "
SELECT
  Products.* ,
  /* No orders will print 'no-orders' in a pseudo column called has_orders */
  CASE WHEN Orders.ProductID IS NULL THEN 'no-orders' ELSE 'has-orders' AS has_orders
FROM
  Products
  LEFT JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";

$result = mysqli_query($con, $sql);
if ($result) {
  $results = array();
  while ($row = mysqli_fetch_array($result)) {
    $results[] = $row;
  }
}
// Now $results is a 2D array of all your Products
// And the column $row['has_orders'] will tell you if it has any...

In this case, you may test in a loop over your rowset whether it has orders:

foreach ($results as $r) {
  if ($r['has_orders'] == 'has-orders') {
    // this has orders
  }
  else {
    // it doesn't...
  }
}

2 Comments

Hi thanks for helping, but I am have some questions, bc I will still have some actions in "else statement", if i put "while" statement inside the "if" statement, how could I process to "else" statement which required to process the result that does not in the array? Thanks :-)
@JamesChen The rows have all been stored into the array $results, so you can check while looping over it. This isn't much different than your original attempt. Added above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.