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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php