简体   繁体   English

in_array函数的一些问题

[英]some problems with in_array function

Hi I am trying to use in_array, I think my syntax is correct, but it says "Wrong datatype for second argument" 嗨,我正在尝试使用in_array,我认为我的语法是正确的,但是它说“第二个参数的数据类型错误”

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. 我的想法是找出“产品表”中的ProductID是否在“订单表”中。

Could someone helps me, Thanks :-) 有人可以帮助我,谢谢:-)

$filter isn't an array; $filter不是数组; it's a mysqli_result object: 这是一个mysqli_result对象:

$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: 我认为您想对其进行迭代,将每个ProductID添加到新数组,然后将该数组传递给in_array函数,如下所示:

$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))
    {

    }

}

Your code is failing because $filter is a MySQLi result resource, not an array. 您的代码失败,因为$filter是MySQLi结果资源,而不是数组。 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. 如果Orders中不存在ProductID ,则INNER JOIN将其从结果集中排除。

$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: 相反,如果要检索所有 Products ,并仅指示其是否有有效的订单,请使用LEFT JOIN并测试SELECT列表中Orders.ProductID是否为null:

$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...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM