我正在尝试基于我从先前选择中获得的嵌套数组进行选择。
这是我第一次选择的地方:
$coursequery = "
SELECT
courseID
FROM enrollments
WHERE
studentID = '$userid'
";
try
{
$stmt = $db->prepare($coursequery);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
$_SESSION['studentcourses'] = $rows;
这将以下列格式获取所有courseID:
Array ( [0] => Array ( [courseID] => 6 ) [1] => Array ( [courseID] => 7 ) )
我希望能够访问这些ID以从其他表中选择信息。我开始尝试使用for循环来获取所有“课程信息”,具体取决于ID。
for($i = 0; $i < $count; $i++) {
$coursequery = "
SELECT
*
FROM courses
WHERE courseID = '$studentcourses[$i]'
";
try
{
$stmt = $db->prepare($coursequery);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetchAll();
$_SESSION['studentcourseinfo'] = $row;
}
在完成此任务时,我们将非常感谢您的帮助!
答案 0 :(得分:1)
您可以使用一个SQL查询来完成此操作,从而消除所有这些循环
SELECT
*
FROM courses
INNER JOIN enrollments ON
enrollments.courseID = courses.courseID
AND enrollments.studentID = '$userid'
WHERE 1
答案 1 :(得分:0)
您可以在查询中使用相同的数组,可以使用implode函数
OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
//Do something once the key is pressed
static_cast<MainWindow*>(userData)->new_screenshot();
return noErr;
}
只要更改输出格式
即可 $coursequery = "SELECT * FROM courses
WHERE courseID IN (" . implode(",", $studentcourses) . ");";
其中6和7将是课程的ID
同样在你的代码中有重写var Array (6, 7)
的错误,这些错误一次覆盖同一个变量