【问题标题】:PHP - foreach function with arrayPHP - 带数组的 foreach 函数
【发布时间】:2014-02-08 04:49:09
【问题描述】:

您好,我在调用函数时出错。

"警告:C:\xampp\htdocs\blog\posts.php 中的非法字符串偏移 'id' 在第 28 行 2"

功能:

function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        return array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
    }

呼叫:

require_once "functions.php";
    $_posts = get_short_posts();
    foreach($_posts as $_post) {
        echo $_post['id'];
    }

【问题讨论】:

  • 尝试在您的 while 循环中执行 die(var_dump($row));。这将停止所有其他执行,并在屏幕上显示错误。
  • 你知道你在 get_short_posts 函数的第一次迭代之后返回,所以 foreach 不会按预期工作。
  • 请显示您的表格列名。也许列“id”称为“Id”或“ID”?
  • 另外,试试:$result = mysql_query($sql)or die(mysql_error());

标签: php arrays function foreach while-loop


【解决方案1】:

你知道你在 get_short_posts 函数的第一次迭代之后返回,所以 foreach 不会按预期工作。

试试:

<?php 
function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    $return = array();
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        $return[] = array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
    }
    return $return;
}
?>

<?php 
require_once "functions.php";

foreach(get_short_posts() as $_post) {
    echo $_post['id'];
}
?>

另外,Don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

【讨论】:

    【解决方案2】:
    function get_short_posts() {
        $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
        $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
            $timestamp = new DateTime($row['date']);
            $data [] = array (
                "id" => $row['id'],
                "title" => $row['title'],
                "content" => $row['content'],
                "author" => $row['author'],
                "date" => $timestamp->format('d-m-Y'),
                "time" => $timestamp->format('H:i')
            );
    
        }
        return $data;
        }
    

    您返回数据,因此循环停止,将您的数据保存在一个数组中,然后像 abive 代码一样返回该数组

    【讨论】:

      【解决方案3】:

      你的代码是错误的,它应该如下,假设查询返回的数据如上所述。

      function get_short_posts() {
          $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
          $result = mysql_query($sql);
          $rows = array();
          $return_data = array();
          while($row = mysql_fetch_assoc($result)) {
              $timestamp = new DateTime($row['date']);
              $data = array (
                  "id" => $row['id'],
                  "title" => $row['title'],
                  "content" => $row['content'],
                  "author" => $row['author'],
                  "date" => $timestamp->format('d-m-Y'),
                  "time" => $timestamp->format('H:i')
              );
              $return_data[] = $data;
          }
          return $return_data ;
      
      }
      
      
      require_once "functions.php";
          $posts = get_short_posts();
          foreach($posts as $key=>$val) {
              echo $val['id'];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 2012-11-02
        • 2017-05-12
        相关资源
        最近更新 更多