如何在PHP中为MySQL查询提取数组键的值

问题描述:

I have here a SQL query to get course information from the data base. The variable courses is an array saved by Session from the previous page. Here is what courses[] looks like if I var_dump it. I have selected 3 courses to be put into the session.

array(3) 
{ 
[0]=> string(7) "CAD8405" 
[1]=> string(7) "CON8413" 
[2]=> string(7) "ENG8328" 
}

My $selectedYear variable varies between 2012 to 2014. The is also saved by session. Below is where I have the problem. My query is fine, there is no need to check that. I cannot get $courseCode to show up as my Course.CourseCode='$courseCode', therefore my query doesn't work.

This is what I found to be where it gets kinda confusing. The $courses array returns a normal array. but when I do $courses[0] = $courseCode; then var_dump($courseCode) = null This is what leads me to believe that data I need as my course ID's are the keys to the $courses[] array. When trying to access the $courses[1], the code thinks its a multidimensional array with the key as the first data. But I could be wrong Is there better way to extract the key of my array?

for($i = 0; $i < count($courses); $i++)
{
    //$courses[$i] = $courseCode;
    $courseCode = $courses[$i];
    $comfirmationString = "SELECT Course.CourseCode, Course.Title, Course.WeeklyHours, Semester.SemesterCode FROM Course, Semester, CourseOffer
    WHERE Semester.YearNum='$selectedYear' AND Course.CourseCode='$courseCode'
    AND Course.CourseCode=CourseOffer.CourseCode AND Semester.SemesterCode=CourseOffer.SemesterCode";

if($comfirmationResult = mysqli_query($link, $comfirmationString))
    {
        while($row = mysqli_fetch_assoc($comfirmationResult))
        {
            echo "<tr><td>$row[CourseCode]</td><td>$row[Title]</td><td>$row[WeeklyHours]</td><td>$row[SemesterCode]</td></tr>";
        }
    }
    else
    {
        echo "<p>Query error: " + mysqli_error($link) + "</p>";
    }
}

Below is the code to show how I have saved the $courses[] variable: I get the results of a query, and if the check box is checked, this courseID gets thrown into the courses[] array.

if($Result2013 = mysqli_query($link, $string2013))
    {
        echo "<form action='CourseSelection.php' method='get'>
        <table><tr><th>Code</th><th>Course Title</th><th>Hours</th><th>Term</th><th>Select</th></tr>";
        while($row2013 = mysqli_fetch_assoc($Result2013))
        {
            echo "<tr><td>$row2013[CourseCode]</td><td>$row2013[Title]</td><td>$row2013[WeeklyHours]</td>
            <td>$row2013[Term]</td><td><input type='checkbox' name='courses[]' value=$row2013[CourseCode]></td></tr>";
        }
        echo "</table>";
    }

You might want to modify your query to associate the compound values to an easier to read value, like so:

$comfirmationString = "SELECT Course.CourseCode as courseCode, Course.Title as courseTitle, Course.WeeklyHours as courseWeeklyHours, Semester.SemesterCode as semesterCode FROM Course, Semester, CourseOffer
WHERE Semester.YearNum='$selectedYear' AND Course.CourseCode='$courseCode'
AND Course.CourseCode=CourseOffer.CourseCode AND Semester.SemesterCode=CourseOffer.SemesterCode";

You also need to wrap your variables in brackets, like so:

echo "<tr><td>{$row['CourseCode']}</td><td>{$row['courseTitle']}</td><td>{$row['courseWeeklyHours']}</td><td>$row[SemesterCode]</td></tr>";

and so on.