PHP函数返回mysql_fetch_assoc()导致基于索引的数组?
这是我在这里发表的第一篇文章,过去我发现我所有的问题都已经解决了。 这次不行,所以请帮助我:-)!
- 为什么 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法 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.