2
votes

I'am at loss here. Not sure why I'm getting "stdClass". Shouldn't I be getting name from 1st column?

$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
1
The code looks correct, can you do a get_class($result)?Mike Purcell
get_class($result) gets me: stdClass --- col.1 (used for classname) is varchar, so it should be ok (format-wise) - no breaks or spaces etc.Jeffz
Figured as much... weird, code looks like it should work.Mike Purcell
Yes it is:) I may try some other PHP versions, to see if there is a difference, but not today.Jeffz
v.5.2.10 gets the same thing ... interestingJeffz

1 Answers

0
votes

The issue is most likely that the class is not defined (or in scope). I did some testing using local data.

$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]' ) 

By adding this above the PDO code

Class levi{

}

I was able to get it to return:

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