• <i id='v6usa'><tr id='v6usa'><dt id='v6usa'><q id='v6usa'><span id='v6usa'><b id='v6usa'><form id='v6usa'><ins id='v6usa'></ins><ul id='v6usa'></ul><sub id='v6usa'></sub></form><legend id='v6usa'></legend><bdo id='v6usa'><pre id='v6usa'><center id='v6usa'></center></pre></bdo></b><th id='v6usa'></th></span></q></dt></tr></i><div id='v6usa'><tfoot id='v6usa'></tfoot><dl id='v6usa'><fieldset id='v6usa'></fieldset></dl></div>

    <legend id='v6usa'><style id='v6usa'><dir id='v6usa'><q id='v6usa'></q></dir></style></legend>
  • <tfoot id='v6usa'></tfoot>

      <small id='v6usa'></small><noframes id='v6usa'>

        <bdo id='v6usa'></bdo><ul id='v6usa'></ul>

        将数组转换为单独的字符串

        时间:2023-09-24

        1. <i id='80flS'><tr id='80flS'><dt id='80flS'><q id='80flS'><span id='80flS'><b id='80flS'><form id='80flS'><ins id='80flS'></ins><ul id='80flS'></ul><sub id='80flS'></sub></form><legend id='80flS'></legend><bdo id='80flS'><pre id='80flS'><center id='80flS'></center></pre></bdo></b><th id='80flS'></th></span></q></dt></tr></i><div id='80flS'><tfoot id='80flS'></tfoot><dl id='80flS'><fieldset id='80flS'></fieldset></dl></div>
                <tbody id='80flS'></tbody>
              <tfoot id='80flS'></tfoot>

                <legend id='80flS'><style id='80flS'><dir id='80flS'><q id='80flS'></q></dir></style></legend>
                • <bdo id='80flS'></bdo><ul id='80flS'></ul>

                  <small id='80flS'></small><noframes id='80flS'>

                • 本文介绍了将数组转换为单独的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I have the following arrays and I would like to convert each one of them into individual strings. In other words, break the array into individual pieces.

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

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

                  This is because I would like to include the individual strings in the following query "

                    $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 相比,而不是视频,博客或文章.

                  This is because format expect an individual string for comparison, such as lets assume the array is ["video", "blogs", "articles"], it wouldn't work if format was to be compared with video,blogs,articles but rather video, blogs or articles.

                  我希望这很清楚,如有任何澄清,请提供建议.

                  I hope this is clear, and for any clarification, please advise.

                  一切顺利,

                  更新:

                  $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']
                          );
                        }
                  

                  推荐答案

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

                  I think this would do it. This also will resolve the injection hole by using prepared statements.

                  $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';
                  }
                  

                  给你一个查询

                  select * from resources where stage LIKE :stage0 or stage LIKE :stage1 or topic LIKE :topic0 or topic LIKE :topic1 or topic LIKE :topic2 or topic LIKE :topic3

                  select * from resources where stage LIKE :stage0 or stage LIKE :stage1 or topic LIKE :topic0 or topic LIKE :topic1 or topic LIKE :topic2 or topic LIKE :topic3

                  和绑定值:

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

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

                  Here's the initial code I had for when I thought the data was paired..

                  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

                  A few links on prepared statements:
                  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

                  这篇关于将数组转换为单独的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 代码 (mysqli) 查询在特定代码后不起作用 下一篇:Mysqli 不允许多个查询?

                  相关文章

                  最新文章

                  <small id='BoI1C'></small><noframes id='BoI1C'>

                • <i id='BoI1C'><tr id='BoI1C'><dt id='BoI1C'><q id='BoI1C'><span id='BoI1C'><b id='BoI1C'><form id='BoI1C'><ins id='BoI1C'></ins><ul id='BoI1C'></ul><sub id='BoI1C'></sub></form><legend id='BoI1C'></legend><bdo id='BoI1C'><pre id='BoI1C'><center id='BoI1C'></center></pre></bdo></b><th id='BoI1C'></th></span></q></dt></tr></i><div id='BoI1C'><tfoot id='BoI1C'></tfoot><dl id='BoI1C'><fieldset id='BoI1C'></fieldset></dl></div>

                  1. <tfoot id='BoI1C'></tfoot>
                      <legend id='BoI1C'><style id='BoI1C'><dir id='BoI1C'><q id='BoI1C'></q></dir></style></legend>

                        <bdo id='BoI1C'></bdo><ul id='BoI1C'></ul>