2012-08-09 66 views
1

我與PHP函數問題「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; 
} 

當我運行該功能,返回數組應該是這樣的:

[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 
      ) 

但它看起來像這樣:

[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 
      ) 

我的數組的第二個元素總是空的。無論使用哪個sql查詢。有人知道這個問題嗎?

+2

您是否驗證數據庫中的內容?另外,請不要使用已棄用的'mysql_X'函數,要麼移動到PDO,要麼使用'mysqli_X'函數。 – Geoffrey 2012-08-09 11:41:25

+0

是的,我驗證了內容。我想,這是由內容引起的。但是這個函數總是返回這個結果。 – Patrick 2012-08-09 11:43:34

+0

你可以發佈你的結果使用的'$ query'嗎? – dunc 2012-08-09 11:49:18

回答

0

出於測試目的,請嘗試(這是一些調試輸出和一些樣板代碼)

<?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)); 
} 

我的機器上打印

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" 
    } 
} 
+0

我想我得到了我的問題!如果我早一點打印出結果是正確的。 - > mysql_fetch_assoc()正常工作。所以我的錯誤是在另一個位置。 – Patrick 2012-08-09 12:10:50