gpt4 book ai didi

php - 我可以在 PHP 中使用 for 或 while 循环这段代码吗?

转载 作者:行者123 更新时间:2023-11-29 21:12:33 26 4
gpt4 key购买 nike

<?php
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 1";
$query1 = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 2";
$comments = mysql_query($query);
$comments1 = mysql_query($query1);
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
while($row = mysql_fetch_array($comments1, MYSQL_ASSOC)) {
$bobot1 = $row['bobot'];
$bobot1 = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
?>

我想让这段代码可以循环到10。我希望没有太多变量,例如:$query,$query1,$query2,...,$query10,$comments,$comments1,$comments2 , ..., $comments10, $bobot, $bobot1, $bobot2, ..., $bobot10。请有人帮助我...

最佳答案

你就快到了。但我不得不提一下,您应该开始使用参数化查询 prepared statements而不是手动构建查询。

$id = 1;
while($id <= 10) {
// construct your query
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = $id";
// execute and get results
$comments = mysql_query($query);

// iterate over records in result
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}

// increment the id for next cycle through the loop
$id = $id + 1;
}

关于php - 我可以在 PHP 中使用 for 或 while 循环这段代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com