RE: [PHP] Weird pdo-mysql behavior
| From: | Arno Kuhl | Date: | Thu, 13 Nov 2008 14:01:17 +0000 |
| Subject: | RE: [PHP] Weird pdo-mysql behavior | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283249@lists.php.net to get a copy of this message | ||
Suppose I have two tables Contracts and Clients that are connected using
ClientId field.
This is a stripped sample code that doesn't work:
<?php
function getClientFullName($id,$dbh){
$query = "SELECT * FROM Clients WHERE Id=".$id;
$sthr = $dbh->query($query);
$res = $sthr->fetch(PDO::FETCH_ASSOC);
return $res['Name'];
}
try {
$dbh = new PDO('mysql:host=localhost;port=3306;dbname=ins', 'root',
'', array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_PERSISTENT
=> true));
$sql = "SELECT * FROM Contracts";
$sth = $dbh->query($sql);
print "<pre>";
while($res = $sth->fetch(PDO::FETCH_ASSOC)) {
$name = getClientFullName($res['ClientId'],$dbh);
print $name."<br>";
}
} catch (Exception $e) {
print $e->getMessage();
}
?>
And when I say it doesn't work I mean that that I get:
Call to a member function fetch() on a non-object
When calling: getClientFullName
BTW try to top post.
--
Thodoris
------------------
Hi Theodoris
First place I'd look is to see if the sql query was successful.
If it failed you'll get this error.
You can try a simple test
$sth = $dbh->query($sql);
if ($sth == FALSE) {
print "failed";
exit;
}
Arno