【问题标题】:PHP foreach, arrays data used in queryPHP foreach,查询中使用的数组数据
【发布时间】:2011-08-08 14:34:50
【问题描述】:

我有:

$array1 =     //contains places ids and locations;
$array2 = array();
$array3 = array();


  foreach($array1 as $itemz)
  {     
      $array2[] = $itemz[place][id];
      $array3[] = $itemz[place][location][city];

      $sql = "select * from places where id=".$array2." and location=".$array3."";
  }

但是当我打印 $sql 我得到:

  select * from places where id=12 and location=Array

谁能告诉我代码有什么问题?

谢谢!

【问题讨论】:

  • 你期待什么输出?
  • 当 $array2 是一个数组时,我完全不知道他/她是如何得到id=12 的。
  • SQL注入警告:使用mysql_real_escape_string()保护自己。

标签: php mysql arrays foreach


【解决方案1】:

很抱歉,您的代码完全没有意义。我很惊讶你会得到这样的结果。让我们来看看它。

引号在哪里?

$array2[] = $itemz[place][id];
$array3[] = $itemz[place][location][city];

这里缺少引号,请添加它们

$array2[] = $itemz['place']['id'];
$array3[] = $itemz['place']['location']['city'];

数组到字符串的转换

$sql = "select * from places where id=".$array2." and location=".$array3."";

这个语句不应该有两个原因。

  1. 假设 id 是 INT 的单个字段,并且您在 $array2 中有一堆 INT,如果没有 MySQL IN,您仍然无法比较它们。

  2. 您正在从 PHP 数组转换为字符串。那是行不通的。

由于您在循环中运行,$array2[]$array3[] 将继续变化并增长。

所以你实际上想要做的是想出一个像

这样的查询
$sql = "SELECT * 
        FROM places 
        WHERE 
             id IN (" . implode(',', $array2) . ") AND 
             location IN (" . implode(',', $array3) . ")";

但这根本没有意义,因为随着循环的继续,您将逐步检索相同的数据。

所以我认为你真正想做的是

$sql = "SELECT * 
        FROM places 
        WHERE 
             id = {$itemz['place']['id']} AND 
             location = {$itemz['place']['location']['city']}";

这很可能是您需要的。这会在您遍历数组时检索每一行的行。

我会做的一些改进是。

在循环完成后运行一次查询,因此您只需运行一次查询而不是n 次。

另外,考虑只检索您需要的列,而不是执行SELECT *

【讨论】:

  • 我猜你的意思是columns,而不是最后一句中的rows。尽管如此 +1。
  • @m42 列确实,已修复:)
【解决方案2】:

您不能使用 $array3 来构建查询,因为它是一个数组。相反,您可以像下面这样编码 -

 foreach($array1 as $i=>$itemz)
  {     
      $array2[$i] = $itemz[place][id];
      $array3[$i] = $itemz[place][location][city];

      $sql = "select * from places where id=".$array2[$i]." and location=".$array3[$i]."";
  }

【讨论】:

    【解决方案3】:

    这一行:

     $array3[] = $itemz[place][location][city];
    

    导致创建一个名为$array3 的数组并添加一个等于$itemz[place][location][city] 且键为0 的元素。当您尝试将此变量嵌入到查询中时,您会遇到问题,因为它不是字符串。

    你可能需要的是:

     $id = $itemz['place']['id'];
     $city = $itemz['place']['location']['city'];
     $sql = "select * from places where id=".intval($id)." and location='".
            mysql_real_escape_string($city)."'";
    

    请注意,我已进行更改以修复代码的其他一些严重问题(indexing into arrays with constants instead of stringsleaving your code vulnerable to SQL injection)。

    【讨论】:

      【解决方案4】:

      当你只需要一个标准变量时为什么要使用数组:

      $array1 =     //contains places ids and locations;
      
      foreach($array1 as $itemz)
      {     
          $id = $itemz['place']['id'];
          $city = $itemz['place']['location']['city'];
      
          $sql = "select * from places where id='$id' and location='$city'";
      }
      

      【讨论】:

      • 请阅读下面的 JohnP 答案,我没有注意到您的代码中缺少引号。这很重要,您的代码中有一些严重的问题。我也更新了我的代码。
      猜你喜欢
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2014-01-08
      • 1970-01-01
      • 2021-06-02
      • 2018-04-06
      相关资源
      最近更新 更多