【问题标题】:convert an array into individual strings将数组转换为单个字符串
【发布时间】:2015-11-24 23:30:36
【问题描述】:

我有以下数组,我想将它们中的每一个都转换为单独的字符串。换句话说,将数组分解成单独的部分。

  $formatsArray = $_POST['formats'];
      $topicsArray = $_POST['topics'];

这是因为我想在以下查询中包含单个字符串“

  $resources = "select * from resources where
                    stage LIKE '%".$stage."%'
                    AND format LIKE '%".$formats."%'";


      $run_query = mysqli_query($con, $resources);

这是因为 format 需要一个单独的字符串进行比较,例如假设数组是 ["video", "blogs", "articles"],如果 format 要与 video,blogs,articles 进行比较,则它将不起作用,而是视频、博客或文章。

我希望这很清楚,如有任何澄清,请告知。

一切顺利,

更新:

$formats = explode(',', $formatsArray);
      $topics = explode(',', $topicsArray);


      $resources = "select * from resources where
                    stage LIKE '%".$stage."%'
                    AND format LIKE '%".$formats."%' AND topic LIKE '%".$topics."%' ";

更新:

$run_query = mysqli_query($con, $resources);


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

    $data[] = array(
      'format' => $row['format'],
      'title' => $row['title'],
      'costs' => $row['cost'],
      'stage' => $row['stage'],
      'topic' => $row['topic'],
      'link' => $row['link']
    );
  }

更新

  include('db.php');


  $query = 'select * from resources where ';
  $query .= 'stage LIKE :stage and';
  $execute[':stage'] = '%' . $stage . '%';
  if(!empty($_POST['formats'])){
  foreach($_POST['formats'] as $key => $format) {
      $query .= 'format LIKE :format' . $key . ' and ';
      $execute[':format' . $key] = '%' . trim($format) . '%';
  }
  }
  if(!empty($_POST['topics'])){
  foreach($_POST['topics'] as $key => $topic) {
      $query .= 'topic LIKE :topic' . $key . ' and ';
      $execute[':topic' . $key] = '%' . trim($topic)  . '%';
  }
  }
  $query = rtrim($query, ' and ');
  if(!empty($execute)) {
      $stmt = $con->prepare($query);
      $stmt->execute($execute);
  } else {
      echo 'You must search for something';
  }


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

        $data[] = array(
          'format' => $row['format'],
          'title' => $row['title'],
          'costs' => $row['cost'],
          'stage' => $row['stage'],
          'topic' => $row['topic'],
          'link' => $row['link']
        );
      }

【问题讨论】:

  • 所以逗号是分隔符?使用 explode 然后遍历数组并构建您的查询。使用准备好的语句。
  • 感谢您的回复。逗号不是分隔符。在数据库中,您有例如“video”或“blogs”或“articles”,因此如果数组使用逗号(例如 video,blogs,articles)内爆,则它不会匹配任何条目。您能否详细说明您的解决方案
  • 不是内爆,而是爆炸。所以你有$_POST['formats'] 这是video,blogs,articles 对吗?因此,如果您在, 上爆炸然后遍历该数组,您将分别拥有每个术语..
  • 感谢我在最初的帖子底部添加了更新。我将如何进行下一步
  • $formats$topics 总是有相同数量的元素?

标签: php mysql arrays mysqli


【解决方案1】:

忽略准备好的语句的必要性,您可以这样做:

  $formats = implode('","', $formatsArray);
  $topics = implode('","', $topicsArray);

  $resources = "select * from resources where
                stage LIKE '%".$stage."%'
                AND format IN(".$formats.") AND topic IN(\"".$topics."\") ";

当你implode每个数组时,通过在每个,之前和之后添加",你的数组就会变成例如

video","blogs","articles

因此,我们需要将" 添加到每个IN 列表的开头和结尾。这将使最终查询如下:

select * from resources where
stage LIKE '%".$stage."%'
AND format IN("video","blogs","articles") AND ...

【讨论】:

  • $formatsArray 不是 OPs 帖子中的 PHP 数组。先引爆,然后用引号引爆。此外,在查询中,您已经转义了 $topics 周围的引号,而不是 $formats 周围的引号。这可能会令人困惑。
  • 谢谢你,@onik——我猜变量名让我失望了。 :P 丢失的转义引号完全是我的错,感谢您的关注。
  • 谢谢。我不幸得到以下错误:trim() 期望参数是字符串,给定数组
  • @user3907211 很抱歉——我以为你的$formatsArray 字符串。答案已编辑,现在应该可以工作了——尽管看起来@chris85 的答案对你有用。
【解决方案2】:

我认为这样做可以。这也将通过使用准备好的语句解决注入漏洞。

$query = 'select * from resources where ';
if(!empty($_POST['formats'])){ 
foreach($_POST['formats'] as $key => $format) {
    $query .= 'stage LIKE :stage' . $key . ' or ';
    $execute[':stage' . $key] = '%' . trim($format) . '%';
}
}
if(!empty($_POST['topics'])){
foreach($_POST['topics'] as $key => $topic) {
    $query .= 'topic LIKE :topic' . $key . ' or ';
    $execute[':topic' . $key] = '%' . trim($topic)  . '%';
}
}
$query = rtrim($query, ' or ');
if(!empty($execute)) {
    echo $query;
    print_r($execute);
    //$stmt = $mysqli->prepare($query);
    //$stmt->execute($execute);
} else {
    echo 'You must search for something';
}

给你一个查询

从资源中选择 *,其中 stage LIKE :stage0 或 stage LIKE :stage1 或 topic LIKE :topic0 或 topic LIKE :topic1 或 topic LIKE :topic2 或 topic LIKE :topic3

和绑定值:

Array
(
    [:stage0] => %test%
    [:stage1] => %test1%
    [:topic0] => %value1%
    [:topic1] => %value2%
    [:topic2] => %value3%
    [:topic3] => %value4%
)

这是我认为数据已配对时的初始代码..

foreach($formats as $key => $format) {
    $topic = $topics[$key];
    $query .= '(stage LIKE :stage' . $key . ' and topic LIKE :topic' . $key . ') or ';
    $execute[':stage' . $key] = '%' . trim($format) . '%';
    $execute[':topic' . $key] = '%' . trim($topic)  . '%';
}

关于准备好的语句的一些链接:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
http://php.net/manual/en/mysqli.prepare.php
http://php.net/manual/en/mysqli-stmt.execute.php

【讨论】:

  • 谢谢。对prepared statement不太熟悉。为什么 //$stmt = $mysqli->prepare($query); //$stmt->execute($execute);注释掉
  • 哦,你可以取消注释,这只是一个如何使用它的例子。我没有设置数据库,所以无法执行。
  • 我应该注释掉 echo $query; print_r($execute);
  • 谢谢。只是一个小要求。因为我不太熟悉准备好的声明,所以我省略了以下代码,我在最初的帖子下添加了。是否必须对它们进行任何更改?
  • 好的,首先请不要接受我的回答。当它有效时,您应该接受答案。继续前进,您指的是什么数组,$_POST['formats'] 不是字符串吗?如果不是,那么$formats = explode(',', $formatsArray); 是如何工作的?
猜你喜欢
  • 2016-04-02
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
相关资源
最近更新 更多