获取错误中的 pdo 获取

发布于 2024-09-25 07:19:39 字数 597 浏览 7 评论 0原文

我正在尝试运行以下脚本(未成功):

$variables = $db->query("SELECT * FROM table1 WHERE Session_ID = '$sess1'");

while($row = $variables->fetch()) {

//FETCH DATA
$id= $row["ID"];
$info = $db->query("SELECT * FROM table2 WHERE ID = $id");

while($row2 = $info->fetch()) { 
    $name = $row2["FNAME"]." ".$row2["LNAME"]; }
    $phone = $row2["PHONE"];
}

//SEND CUSTOMER EMAIL
require("../email/email.php");                      
}

这会返回错误:致命错误:在...中的非对象上调用成员函数 fetch()

虽然我能够“解决”这个问题,这很丑陋。本质上,我必须在下面尝试的那个之前拨打几个电话(理论上应该可行)。

有什么想法吗?

I am trying to run the following script (unsuccessfully):

$variables = $db->query("SELECT * FROM table1 WHERE Session_ID = '$sess1'");

while($row = $variables->fetch()) {

//FETCH DATA
$id= $row["ID"];
$info = $db->query("SELECT * FROM table2 WHERE ID = $id");

while($row2 = $info->fetch()) { 
    $name = $row2["FNAME"]." ".$row2["LNAME"]; }
    $phone = $row2["PHONE"];
}

//SEND CUSTOMER EMAIL
require("../email/email.php");                      
}

this returns the error: Fatal error: Call to a member function fetch() on a non-object in...

While I am able to "solve" the problem, it is ugly. Essentially I have to make several calls ahead of the one I'm trying below (which in theory should work).

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-10-02 07:19:39

据我所知,您必须先从结果集中获取所有结果,然后才能发出新查询。如果您使用 MySQL,您可以通过调用 $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true); 来规避这一点,但我建议不要这样做,因为它会使您的代码不太可移植,并且 PDO 的主要优点(数据库抽象)丢失了。

As far as I can tell, you have to fetch all results from a resultset before you can issue a new query. If you're using MySQL, you can circumvent that by calling $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);, but I would advice against it, as it makes your code less portable, and on of the major plusses of PDO, database-abstraction, is lost.

漫雪独思 2024-10-02 07:19:39

试试这个

    //prepare the query
    $variables = $db->prepare("SELECT * FROM table1 WHERE Session_ID = :sess1");
    $info = $db->prepare("SELECT * FROM table2 WHERE ID = :ID");

    //bind the variable :sess1 to $sess1
    $variables->bindParam(':sess1', $sess1, PDO::PARAM_STR);

    //execute the query 
    $variables->execute;

    //fetch the result
    $result = $variables->fetch(PDO::FETCH_ASSOC);

    //set $result['ID'] to a variable and bind it to the variable :ID in our second query
    $id = $result['ID'];
    $info->bindParam(':ID', $id, PDO::PARAM_INT);

    while($row2 = $info->fetch()) { 
        $name = $row2["FNAME"]." ".$row2["LNAME"]; }
        $phone = $row2["PHONE"];
    }

    //SEND CUSTOMER EMAIL
    require("../email/email.php");                      
    }

try this instead

    //prepare the query
    $variables = $db->prepare("SELECT * FROM table1 WHERE Session_ID = :sess1");
    $info = $db->prepare("SELECT * FROM table2 WHERE ID = :ID");

    //bind the variable :sess1 to $sess1
    $variables->bindParam(':sess1', $sess1, PDO::PARAM_STR);

    //execute the query 
    $variables->execute;

    //fetch the result
    $result = $variables->fetch(PDO::FETCH_ASSOC);

    //set $result['ID'] to a variable and bind it to the variable :ID in our second query
    $id = $result['ID'];
    $info->bindParam(':ID', $id, PDO::PARAM_INT);

    while($row2 = $info->fetch()) { 
        $name = $row2["FNAME"]." ".$row2["LNAME"]; }
        $phone = $row2["PHONE"];
    }

    //SEND CUSTOMER EMAIL
    require("../email/email.php");                      
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文