我想在我的phpmyadmin表中获取特定值的ID。 因此,我得到了一个以'id_cal'作为AI的表。 id,“ mois”代表带有数字的月份(例如1月为1),“ annee”代表年份。 (请参阅calendar table)
我正在尝试为月份和年份设置php变量,如果它们与当前月份和年份匹配,我想获取此特定ID。
我在遇到麻烦的地方注释了php代码,这里是:
<?php
include_once('config.php');
$m = idate('n');
$y = idate('Y');
echo $m; echo "\t"; echo $y; echo "<br>"; echo "<br>"; // The result of this is 7 2019
$reponse = $bdd->query('SELECT * FROM calendrier');
while($donnees= $reponse->fetch()){
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
echo $id_cal;
echo "\t";
echo $mois;
echo "\t";
echo $year;
echo "<br>";
}
// What I am trying to do :
if (($m = $mois) && ($y = $year)){ // If the month and the year are the current month/year
$i = $id_cal; // I want to put the id refering to the current month/year (in my phpmyadmin table) into a new variable
echo "<br>"; // and echo this variable (or use it in other ways)
echo $i; // BUT what I am echoing is 24 representing the number of values in my array
} // How can I only get 7 ? (in this exemple, since we are the 7/2019)
这是我在本地主机中得到的内容:echo
我真的不明白为什么我没有7。
此外,我尝试了此方法,而不是我的一段时间:
$donnees= $reponse->fetch();
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
// But in this cas I am having $i = 1, so it's the same problem.
在此先非常感谢您的回复,对此我感到很努力。
答案 0 :(得分:1)
这是因为在您的while语句的每次迭代中,id_cal
被新值id_cal
覆盖。
要获得所需的结果,可以将if放在while语句中...
while($donnees= $reponse->fetch()){
$mois = $donnees['mois'];
$year = $donnees['annee'];
$id_cal = $donnees['id_cal'];
echo $id_cal;
echo "\t";
echo $mois;
echo "\t";
echo $year;
echo "<br>";
if (($m == $mois) && ($y == $year)){
$my_var_to_use_elsewhere = $id_cal;
}
}
echo "<br>";
echo $my_var_to_use_elsewhere;