从SQL数据库中获取数组形式的值,并基于该数组执行搜索操作


Getting values as array from SQL Database and performing a search operation based on that array

我正试图根据WHERE条件从数据库中获取几个数组,并需要根据该数组从另一个表中执行搜索。我正在努力做到这一点,但我遇到了几个错误。

代码为:

 $sql = "SELECT post_id FROM flat_booking where buyer_mobile='$mobile'";
 $result = mysqli_query($con,$sql);
 $post_id = array();
 while(($row = mysqli_fetch_assoc($result))) {
 $post_id[] = $row['id'];    
 }

 $sql = "SELECT id,area,furnished,image,numberofrooms,price,type FROM flatowner where id IN '$post_id'";

也许这条线抛出了错误:

$post_id[] = $row['id']; 

您可以尝试使用JOINS来减少您必须执行的查询量,并且在后续查询中不必传入逗号分隔的列表。请尝试下面的查询。

SELECT
  O.id,
  O.area,
  O.furnished,
  O.image,
  O.numberofrooms,
  O.price,
  O.type
FROM flatowner O
  INNER JOIN flat_booking B ON O.id = B.post_id
WHERE B.buyer_mobile = '$mobile'