我有这个PHP代码从数据库表中选择studentid并在Project1列中打印出值为1的学生。
<?PHP
include_once("connectionToProjectChoice.php");
/* Check column 1 (Project1) in tbl_studentchoice*/
$check = "SELECT * FROM tbl_studentchoice WHERE Project1 = '1'";
$result = mysqli_query($conn, $check);
if ($result->num_rows > 0){ //check
$student = array();
while($row = mysqli_fetch_assoc($result)) {
$array[$row['studentid']]=$row['Project1'];
}
}
print_r($array);
?>
所以我的问题是有没有办法可以单独存储studentname(c125582333)和(c12558242)所以我可以创建一个If语句,因为我想在代码的下一部分比较它们。
答案 0 :(得分:0)
if ($result->num_rows > 0){ //check
$student = array();
while($row = mysqli_fetch_assoc($result)) {
$array[] = [
'studentid' => $row['studentid']],
'Project1' => $row['Project1']
];
}
}
print_r($array);
以不同的方式构建数组可能会更容易吗?
答案 1 :(得分:0)
<?PHP
include_once("connectionToProjectChoice.php");
/* Check column 1 (Project1) in tbl_studentchoice*/
$check = "SELECT * FROM tbl_studentchoice WHERE Project1 = '1'";
$result = mysqli_query($conn, $check);
if ($result->num_rows > 0){ //check
$i = 1;
$students = array();
while($row = mysqli_fetch_assoc($result)) {
$students['student'.$i++]=$row['studentid'];
}
}
print_r($students);
extract($students); //gives you $student1, $student2, ...
?>