简体   繁体   English

PHP函数“ mysql_fetch_Assoc”的错误结果

[英]Wrong result with PHP function 'mysql_fetch_Assoc'

I have a problem with the PHP function 'mysql_fetch_assoc()' 我的PHP函数'mysql_fetch_assoc()'有问题

public function result ($query) {
    $result = false;
    if (is_resource($this->resourceHandler)) {
        $result = array();
        while ($tempVar = @mysql_fetch_assoc($query)) {
            $result[] = $tempVar;
        }
    }else{
        throw new Exception ('Keine Verbindung zur Datenbank vorhanden!');
    }
    return $result;
}

When i run this function the array returned should look like this: 当我运行此函数时,返回的数组应如下所示:

[0] => Array
            (
                [id] => 1
                [description] => Test
                [date] => 2012-08-09
            )
[1] => Array
            (
                [nummer] => 2
                [beschreibung] => Test2
                [datum] => 2012-08-09
            )
[2] => Array
            (
                [nummer] => 3
                [beschreibung] => test3
                [datum] => 2012-08-10
            )

But it looks like this: 但它看起来像这样:

[0] => Array
            (
                [id] => 1
                [description] => Test
                [date] => 2012-08-09
            )
[1] => Array
            (
                [nummer] => 
                [beschreibung] => 
                [datum] => 1970-01-01
            )
[2] => Array
            (
                [nummer] => 3
                [beschreibung] => test3
                [datum] => 2012-08-10
            )

The second element of my array is ALWAYS empty. 我数组的第二个元素始终为空。 It's regardless which sql-query is used. 不管使用哪个sql查询。 Does anybody know this problem? 有人知道这个问题吗?

For testing purposes please try (it's your code with some debug output and some boilerplate) 出于测试目的,请尝试(这是您的代码,带有一些调试输出和一些样板)

<?php
$mysql = mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test', $mysql) or die(mysql_error($mysql));

echo 'mysql_get_client_info: ', mysql_get_client_info(), "\n";
echo 'mysql_get_host_info: ', mysql_get_host_info($mysql), "\n";
echo 'mysql_get_server_info: ', mysql_get_server_info($mysql), "\n";
echo 'mysql_get_proto_info: ', mysql_get_proto_info($mysql), "\n";

setup($mysql);

$query = mysql_query('SELECT * FROM tmpspeisen', $mysql) or die(mysql_error($mysql));
$foo = new Foo($mysql);
var_dump ( $foo->result($query) );

class Foo {
    protected $resourceHandler;

    public function __construct($mysql) {
        $this->resourceHandler = $mysql;
    }

    public function result($query) {
        $result = false;
        if (is_resource($this->resourceHandler)) {
            $result = array();
            while ($tempVar = @mysql_fetch_assoc($query)) {
                $result[] = $tempVar;
            }
        }else{
            throw new Exception ('Keine Verbindung zur Datenbank vorhanden!');
        }
        return $result;
    }
}

function setup($mysql) {
    mysql_query('
        CREATE TEMPORARY TABLE tmpspeisen (
            id int auto_increment,
            description varchar(128),
            `date` Date,
            primary key(id)
        )
    ', $mysql) or die(mysql_error($mysql));

    mysql_query("
        INSERT INTO tmpspeisen (description, `date`) VALUES
        ('Test', '2012-08-09'), ('Test2', '2012-08-09'), ('test3', '2012-08-10')
    ", $mysql) or die(mysql_error($mysql));
}

prints on my machine 在我的机器上打印

mysql_get_client_info: mysqlnd 5.0.10 - 20111026 - $Id: b0b3b15c693b7f6aeb3aa66b646fee339f175e39 $
mysql_get_host_info: localhost via TCP/IP
mysql_get_server_info: 5.5.25a
mysql_get_proto_info: 10
array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    string(1) "1"
    ["description"]=>
    string(4) "Test"
    ["date"]=>
    string(10) "2012-08-09"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(1) "2"
    ["description"]=>
    string(5) "Test2"
    ["date"]=>
    string(10) "2012-08-09"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(1) "3"
    ["description"]=>
    string(5) "test3"
    ["date"]=>
    string(10) "2012-08-10"
  }
}

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

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