在循环中关闭连接

时间:2013-11-03 22:57:49

标签: php

我有以下代码:

/* Get the 10 latest posts from users you're following */
$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();

/* If a result exists, continue. */
if ($result->num_rows) {
    while ($row = $result->fetch_assoc()) {

        /* Get the user's username from their id */
        $stmt = $cxn->prepare('SELECT username FROM users WHERE user_id = ?');
        $stmt->bind_param('i', $row['user_id']);
        $stmt->execute();
        $stmt->bind_result($username);
        $stmt->fetch();
        $stmt->close(); // this is where I'm closing the connection
    }
}

在最后一行,您会注意到我正在关闭while循环中的连接。问题是,如果删除该行,我会在该行上收到错误Fatal error: Call to a member function bind_param() on a non-object

我猜测关闭连接然后再次为循环中的下一个元素重新打开它并不是一件好事。那么,我该如何解决这个问题呢?删除关闭连接线时,为什么会出现此错误?

请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

/* Get the 10 latest posts from users you're following */
$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();

/* If a result exists, continue. */
if ($result->num_rows) {
  while ($row = $result->fetch_assoc()) {

    /* Get the user's username from their id */
    $stmt = $cxn->prepare('SELECT username FROM users WHERE user_id = ?');
    $userid = (int)$row['user_id'];
    $stmt->bind_param('i', $userid); // Forcing $row['user_id'] to int
    $stmt->execute();
    $stmt->bind_result($username);
    $stmt->fetch();
    //$stmt->close();  Don't close it here
  }
}
$stmt->close();

另一种方法,但更耗时的是:

 /* Get the 10 latest posts from users you're following */
$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();

/* If a result exists, continue. */
if ($result->num_rows) {
  while ($row = $result->fetch_assoc()) {

    /* Get the user's username from their id */
    $stmt_loop = $cxn->prepare('SELECT username FROM users WHERE user_id = ?');
    $userid = (int)$row['user_id'];
    $stmt_loop->bind_param('i', $userid); // Forcing $row['user_id'] to int
    $stmt_loop->execute();
    $stmt_loop->bind_result($username);
    $stmt_loop->fetch();
    $stmt_loop->close();  //Close the local stmt here
  }
}
$stmt->close();

答案 1 :(得分:0)

这是我在我的一个Github存储库上解决这个问题的一个例子:

$Query = $Database->prepare("FIRST QUERY");
$Query->execute();
$Query->bind_result($ID,$Password);
$Query->store_result();
   while ($Query->fetch()){

       $Secondary_Query = $Database->prepare("SECOND QUERY");
       $Secondary_Query->bind_param();
       $Secondary_Query->execute();
       $Secondary_Query->close();
   }
$Query->close();

修改此选项以适合您的工作场景,您将有一个成功运行的查询