2012-03-03 103 views
2

我在這裏虧本。不知道爲什麼我得到「stdClass」。我不應該從第一列獲得名字嗎?PDO :: FETCH_CLASSTYPE - 意外的結果

 
$sql = "SELECT cn, iso2, iso3, fid, sort FROM ct"; 
$stmt = $dbh->query($sql); 
$result = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE); 
var_dump($result); 

//expected - first: cn.col holds country names 
object(Brazil)[3] 
    public 'iso2' => string 'BR' (length=2) 
    public 'iso3' => string 'BRA' (length=3) 
    public 'fid' => string '1' (length=1) 
    public 'sort' => string '0' (length=1) 

//received 
object(stdClass)[3] 
    public 'iso2' => string 'BR' (length=2) 
    public 'iso3' => string 'BRA' (length=3) 
    public 'fid' => string '1' (length=1) 
    public 'sort' => string '0' (length=1)var_dump 
+0

的代碼看起來是正確的,你可以做一個'get_class($結果)'? – 2012-03-04 00:05:45

+0

get_class($ result)獲取我:stdClass --- col.1(用於classname)是varchar,所以它應該沒問題(格式明智) - 沒有中斷或空格等。 – Jeffz 2012-03-04 00:28:47

+0

想象得那麼奇怪,代碼看起來應該起作用。 – 2012-03-04 00:44:46

回答

0

問題很可能是該類未定義(或範圍)。我使用本地數據做了一些測試。

$conn = new PDO("mysql:host=localhost;dbname=test", 'user', 'pass'); 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$stmt = $conn->prepare("SELECT name,email FROM `test_table`"); 
$stmt->execute(); 
$result = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE); 

print_r($result) // returns stdClass Object ([email] => '[email protected]') 

通過增加這個PDO以上代碼

Class levi{ 

} 

我能得到它返回:

levi Object ([email] => '[email protected]')