简体   繁体   English

函数只会多次返回一个值

[英]function only returns one value multiple times

I have this function : 我有这个function

function get_content($text_to_match) {
    $query  = "SELECT * ";
    $query .= "FROM table_name ";
    $query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
    $cont = mysqli_query($connection, $query);
    if($content = mysqli_fetch_assoc($cont)) {
        return $content;
    } else {
        return null;
    }
}

But when I call it like: 但是当我这样称呼它时:

  <div>
      <?php
        for ($i = 1; $i < count(get_content("text_to_match")); $i++) {
          echo '<article>' .
                 '<h3>' . get_content("text_to_match")["string1"] . '</h3>'.
                 '<p>' . get_content("text_to_match")["string2"] . '</p>' .
               '</article>';
        }
      ?>
  </div>

I only get the first match in the DB repeated as many times as the number of found items. 我只在DB得到的第一个匹配项重复了所找到项目的数量。

Where have I gone wrong? 我哪里出问题了?

use this code then fetch data properly 使用此代码,然后正确获取数据

 while($content = mysql_fetch_array($cont))
{
   return $content;

 }

Your logic is at fault. 您的逻辑有误。 You are calling get_content function to get all matches for the loop, as well as to get individual elements out of the list. 您正在调用get_content函数来获取循环的所有匹配项,并从列表中获取单个元素。 This is: 这是:

  • bad logic - the 2nd use case doesn't make sense 错误的逻辑-第二个用例没有意义
  • excessive - you shouldn't need to run a database query just to output an already retrieved result 过多-您只需要输出已检索到的结果就不需要运行数据库查询

What you probably want to do is: 您可能想做的是:

foreach (get_content('text_to_match') as $content) {
    echo '<article>';
    echo '<h3>' . $content['string1']  . '</h3>';
    echo '<p>' . $content['string2'] . '</p>';
    echo '</article>';
}

With a few modifications in combination with tips from @ Anant and @ Unix One 's answer, I arrived at this working solution: 结合@ Anant和@ Unix One的提示进行了一些修改,我得出了这个可行的解决方案:

Function definition 功能定义

  function get_content($text_to_match, $multiple=false) {
        $query  = "SELECT * ";
        $query .= "FROM table_name ";
        $query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
        $cont = mysqli_query($connection, $query);
        if ($multiple) {
          $content_array = [];
          while($content = mysqli_fetch_array($cont)) {
            $content_array[] = $content;
          }
          return $content_array;
        } else {
          if($content = mysqli_fetch_assoc($cont)) {
            return $content;
          } else {
            return null;
          }
        }
   }

Function calls 函数调用

<?php
  /* multiple items */
  foreach(get_content("text_to_match", true) as $content) {
    echo '<article>' .
           '<h3>' . $content["string1"] . '</h3>' .
           '<p>' . $content["string2"] . '</p>' .
         '</article>';
  }
?>

<?php
  /* one item */
  echo get_content("text_to_match")["string"];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM