簡體   English   中英

在另一個while循環內的while循環中執行mysqli准備好的語句

[英]Executing mysqli prepared statment within while loop that's within another while loop

我正在努力實現以下目標:

User 1:
     - Alert 1 Email
     - Alert 2 Email
User 2:
     - Alert 1 Email
     - Alert 2 Email

我試圖在另一個運行 mysqli 准備好的語句的 while 循環中使用一個 while 循環來完成此操作,但我無法讓它工作。

代碼:

$stmtAdd = $conn->prepare("INSERT INTO Data (row1, row2, row3, row4) VALUES ('".$row1."', '".$row2."', '".$row3."', '".$row4."')");
$stmtAdd->execute();
$stmtAdd->close();

$stmtUsers = $conn->prepare("SELECT username, setting1 FROM Users");
$stmtUsers->store_result();
if ($stmtUsers->execute() === FALSE) {
    die("Could not execute prepared statement");
} else {
    $stmtUsers->bind_result($user, $setting1);
    while ($stmtUsers->fetch()) {
        /* Check if each user has setting 1 disabled */
        if ($setting1 == '0'){
            /* Check if any alerts exist for each user */
            $stmtUsersAlerts = $conn->prepare("SELECT name, filter, email FROM Alerts WHERE user='".$user."' AND type='1'");
            $stmtUsersAlerts->store_result();
            $stmtUsersAlerts->bind_result($name, $filter, $email);
            while ($stmtUsersAlerts->fetch()) {
                /* Send email */
            }
            $stmtUsersAlerts->close();
        }
    }
    $stmtUsers->close();
}

stmt->store_result() 不能在 stmt->execute() 之前運行。

$stmtUsers = $conn->prepare("SELECT username, setting1 FROM Users");
if ($stmtUsers->execute() === FALSE) {
    die("Could not execute prepared statement");
} else {
    $stmtUsers->store_result();                // After execute()
    $stmtUsers->bind_result($user, $setting1);
    while ($stmtUsers->fetch()) {
        /* Check if each user has setting 1 disabled */
        if ($setting1 == '0'){
            /* Check if any alerts exist for each user */
            $stmtUsersAlerts = $conn->prepare("SELECT name, filter, email FROM Alerts WHERE user='".$user."' AND type='1'");
            $stmtUsersAlerts->execute();        // This line was missing
            $stmtUsersAlerts->store_result();
            $stmtUsersAlerts->bind_result($name, $filter, $email);
            while ($stmtUsersAlerts->fetch()) {
                /* Send email */
            }
            $stmtUsersAlerts->close();
        }
    }
    $stmtUsers->close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM