RE: Weird pdo-mysql behavior

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux