本文介紹了在另一個while循環內的while循環內執行mysqli準備好的語句的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在努力實現以下目標:
I am trying to achieve the following:
User 1:
- Alert 1 Email
- Alert 2 Email
User 2:
- Alert 1 Email
- Alert 2 Email
我試圖在另一個運行 mysqli 準備好的語句的 while 循環中使用一個 while 循環來完成此操作,但我無法讓它工作.
I'm trying to accomplish this with a while loop inside another while loop that is running a mysqli prepared statement, but I cannot get it to work.
代碼:
$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() 之前運行.
stmt->store_result() can not run before 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();
}
這篇關于在另一個while循環內的while循環內執行mysqli準備好的語句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!