PHP函数返回mysql_fetch_assoc()导致基于索引的数组?

发布于 2024-11-15 22:30:53 字数 1158 浏览 2 评论 0原文

这是我在这里发表的第一篇文章,过去我发现我所有的问题都已经解决了。 这次不行,所以请帮助我:-)!

  • 为什么 $returned_assoc_arr 保存基于索引的数组而不是关联数组?
  • 我还尝试使用参考 public function &fetch_assoc($res) 但没有运气?
  • 有什么办法可以解决这个问题吗?我真的需要它成为一个关联数组。

我希望我很好地解释了一切,并且一些高级 PHP 程序员可以对此提供帮助。

这里我们使用代码片段:

file1.php

public function fetch_assoc($res) {
  $assoc_arr = mysql_fetch_assoc($res);
  echo "<pre>";
  print_r($assoc_arr);
  echo "</pre>";
  return $assoc_arr;
}

file2.php

$returned_assoc_arr = $foo->fetch_assoc($res);
echo "<pre>";
print_r($returned_assoc_arr);
echo "</pre>";

file2.php 的输出:

Array
(
    [id] => 42
    [Client] => 1
    [DebtorAccountNumber] => 1234512345
    [OrderDate] => 2001-04-03 02:00:00
    [Status] => 1
    [Comment] => this is a comment
    [CreatedBy] => 1
    [Reference] => 2083137729
)

Array
(
    [0] => 42
    [1] => 1
    [2] => 1234512345
    [3] => 2001-04-03 02:00:00
    [4] => 1
    [5] => this is a comment
    [6] => 1
    [7] => 2083137729
)

this is my first post here, in the past I found all my questions already solved.
Not this time so please help me :-)!

  • Why is $returned_assoc_arr holding an index based array instead of the associative?
  • I also tried it with a reference public function &fetch_assoc($res) but without luck?
  • Is there any way to fix this? I really need this to be an associative array..

I hope I explained everything quite well and some Senior PHP Coders can help with this.

Here we go with the code snippets:

file1.php

public function fetch_assoc($res) {
  $assoc_arr = mysql_fetch_assoc($res);
  echo "<pre>";
  print_r($assoc_arr);
  echo "</pre>";
  return $assoc_arr;
}

file2.php

$returned_assoc_arr = $foo->fetch_assoc($res);
echo "<pre>";
print_r($returned_assoc_arr);
echo "</pre>";

Output from file2.php:

Array
(
    [id] => 42
    [Client] => 1
    [DebtorAccountNumber] => 1234512345
    [OrderDate] => 2001-04-03 02:00:00
    [Status] => 1
    [Comment] => this is a comment
    [CreatedBy] => 1
    [Reference] => 2083137729
)

Array
(
    [0] => 42
    [1] => 1
    [2] => 1234512345
    [3] => 2001-04-03 02:00:00
    [4] => 1
    [5] => this is a comment
    [6] => 1
    [7] => 2083137729
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-11-22 22:30:53

方法 fetch_assoc 是类的成员函数。 (它在包含对象上调用,必须首先实例化)

您在 file2 中使用的是普通函数 fetch_assoc ,它与 file1 中描述的函数绝对不同。

The method fetch_assoc is a member function of a class. (it's invoked on the containing object, which must be instantiated first)

what you are using in file2 is a ordinary function fetch_assoc, which definitely is different than the function described in file1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文