我正在尝试显示当前可用的汽车列表。我对我期望的工作进行了大量的查询,但它没有显示任何汽车,如果我使用var_dump()
输出$available
的值,它给我这个输出:
array(2){[0] => string(1)“1”[“car_car_id”] => string(1)“1”}
以下代码的输出是它回复test
字符串文字3次。
SQL有什么问题?
<?php
$result = mysqli_query($con, "SELECT car_car_id FROM invoice_line WHERE '2017-06-12' > start_date AND '2017-06-12' < end_date OR '2017-06-12' > start_date AND '2017-06-14' < start_date OR '2017-06-12' < start_date AND '2017-06-14' > end_date OR '2017-06-12' != start_date OR '2017-06-14' != end_date OR '2017-06-12' != end_date OR '2017-06-14' != start_date");
$available = mysqli_fetch_array($result);
$product_array= mysqli_query($con, "SELECT * FROM car WHERE id != '$available'");
while($result = mysqli_fetch_array($product_array)){
echo 'test';
}
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="cars.php?action=add&code=<?php echo $product_array[$key]["id"]; ?>">
<div><strong><?php echo $product_array[$key]["brand"]; ?> <?php echo $product_array[$key]["type"]; ?></strong></div>
<div class="product-price"><?php echo "€".$product_array[$key]["price"]; ?></div>
<div><input type="submit" value="toevoegen aan winkelwagen" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
答案 0 :(得分:1)
在第二个SQL查询中使用NOT IN ()
而不是id != '$available'
,并分隔数组的值(例如,使用implode())。
sql有什么问题?
最明显的问题是第二个查询将从第一个查询中获取数组返回的数组直接放入第二个查询中。这一行:
$product_array= mysqli_query($con, "SELECT * FROM car WHERE id != '$available'");
替换$available
时可以概念化为:
$product_array= mysqli_query($con, "SELECT * FROM car WHERE id != 'Array'");
大概期望的条件是 car 表的 id 字段不等于car id数组中的任何值(来自第一个查询)。在SQL中,我们可以使用IN运算符,并结合 NOT 。例如:
WHERE id NOT IN (3,4)
使用PHP生成此方法有多种方法。一种是使用implode()创建逗号分隔的字符串:
$available = array('car_car_id' => 3, 'car_car_id' => 4); //Sample
$ids = implode(', ', $available); //3,4 when $available has those elements
$product_array= mysqli_query($con, "SELECT * FROM car WHERE id NOT IN ($ids)");
您可能会注意到ID是重复的:
array(2){[0] =&gt; string(1)“1”[“car_car_id”] =&gt; string(1)“1”}
这是因为传递给$resulttype的mysql_fetch_array()没有(2 nd )参数,默认为 MYSQLI_BOTH :
$available = mysqli_fetch_array($result);
要仅选择列名称,请使用 MYSQLI_ASSOC
$available = mysqli_fetch_array($result, MYSQLI_ASSOC);
// => Array ( [car_car_id] => 1 )
或仅对于数字索引,请使用MYSQLI_NUM
$available = mysqli_fetch_array($result, MYSQLI_NUM);
// => Array ( [0] => 1 )
Yusef Hassan中提到的the first comment,需要考虑条件。请记住 AND 优先于 OR (请参阅MySQL Operator Precedence)。
之间存在很大差异:
SELECT car_car_id
FROM invoice_line
WHERE '2017-06-12' > start_date AND '2017-06-12' < end_date
OR '2017-06-12' > start_date AND '2017-06-14' < start_date
OR '2017-06-12' < start_date AND '2017-06-14' > end_date
OR '2017-06-12' != start_date OR '2017-06-14' != end_date
OR '2017-06-12' != end_date OR '2017-06-14' != start_date
和
SELECT car_car_id
FROM invoice_line
WHERE ('2017-06-12' > start_date AND '2017-06-12' < end_date)
OR ('2017-06-12' > start_date AND '2017-06-14' < start_date)
OR ('2017-06-12' < start_date AND '2017-06-14' > end_date)
OR ('2017-06-12' != start_date OR '2017-06-14' != end_date)
OR ('2017-06-12' != end_date OR '2017-06-14' != start_date)
通过在SQL中使用子查询(例如SELECT * FROM car WHERE id NOT IN (SELECT car_car_id FROM invoice_line WHERE ...)
)或LEFT JOIN使用car_car_id IS NULL
之类的条件,可能可以避免此处解决的问题。