获取错误中的 pdo 获取
我正在尝试运行以下脚本(未成功):
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您必须先从结果集中获取所有结果,然后才能发出新查询。如果您使用 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.试试这个
try this instead