0

使用下面的代码,我得到错误"Call to undefined method stdClass::fetchObject()"

function getProdDetails2SaveInInvoice($data) {

    global $dbh;

    try {
        $sth=$dbh->prepare("
        SELECT
            AES_DECRYPT('alt_id', ?),
            AES_DECRYPT('prod_name', ?),
            AES_DECRYPT('prod_desc', ?)
         FROM
            products
        WHERE
        prod_id = ?
    ");

        $sth->execute($data);
        $rs = $sth->query(PDO::FETCH_ASSOC);

        return $rs;
    }

    catch(PDOException $e) {
        echo  "Something went wrong. Please report this error.\n";
        file_put_contents(
            $_SERVER['DOCUMENT_ROOT']."/PDOErrors.txt",
            "\n\nScript name : ".SCRIPT."\nFunction name : ".__FUNCTION__."\n".
            $e->getMessage(), FILE_APPEND);

        throw new failedTransaction();
    }
}

$data = array(
    DBKEY,                  /* field 1                  */
    DBKEY,                  /* field 2                  */
    DBKEY,                  /* field 3                  */
    $prodid             /* comparison               */
);
$rs = getProdDetails2SaveInInvoice($data);

while ($row = $rs->fetchObject()) {
    echo $row->prod_name;
}

不幸的是,这不起作用并返回上述错误。

我可以确认 $dbh 数据库连接正在工作,因为它与用于插入和更新的连接相同。谢谢。

更新

这就是我根据以下建议修改代码的方式,但我仍然没有得到任何返回:

try {
    $dbh    = new PDO("mysql:host=".CO_DB_HOST.";dbname=".CO_DB_NAME, CO_DB_UNAME, CO_DB_PWORD);

    $dbh    ->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
    echo $e->getMessage();
}

function getProdDetails2SaveInInvoice($data) {

    global $dbh;

    try {
        $sth=$dbh->prepare("
        SELECT
            AES_DECRYPT('alt_id', ?),
            AES_DECRYPT('prod_name', ?),
            AES_DECRYPT('prod_desc', ?)
         FROM
            products
        WHERE
            prod_id = ?
    ");

        $sth->execute($data);

        while ($row = $sth->fetchObject()) {
            // PROCESS ROW
            $rs = array($row->alt_id, $row->prod_name, $row->prod_desc);
        }
        return $rs;
    }

    catch(PDOException $e) {
        echo  "Something went wrong. Please report this error.\n";
        file_put_contents(
            $_SERVER['DOCUMENT_ROOT']."/PDOErrors.txt",
            "\n\nScript name : ".SCRIPT."\nFunction name : ".__FUNCTION__."\n".
            $e->getMessage(), FILE_APPEND);

        throw new failedTransaction();
    }
}

// Fetch additional info from invoice_products.
$data = array(
    DBKEY,                  /* field 1                  */
    DBKEY,                  /* field 2                  */
    DBKEY,                  /* field 3                  */
    $prodid                 /* comparison               */
);
$rs = getProdDetails2SaveInInvoice($data);

print_r($rs);

如果我硬编码 'where' 参数 (19),它仍然无法检索结果。理想情况下,我认为我应该在一个对象中检索结果,以便可以对其进行流式传输,但是现在,即使它装在盒子里,我也会很高兴!

数据肯定存在于数据库中,并且可以使用传统查询来提取。

这是 print_r($rs) 的输出:

数组( [0] => [1] => [2] => )

4

1 回答 1

2

您应该循环准备好的语句,而不是调用query准备好的语句。准备好的语句的基本用法如下:

$sth = $dbh->prepare("YOUR QUERY");
$sth->execute();
$results = array();
while ($row = $sth->fetchObject()) {
    $results[] = array(
        'alt_id' => $row->alt_id,
        'prod_name' => $row->prod_name,
        'prod_desc' => $row->prod_desc
    );
}

return $result;

作为替代方案,您还可以使用$sth->fetchAll()which 返回一个数组,其中包含作为查询结果的所有行,请参阅PDOStatement::fetchAll()

于 2012-12-05T23:06:08.393 回答