douwei9973 2012-10-30 03:33
浏览 92
已采纳

如何在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>";
    }
  • 写回答

1条回答 默认 最新

  • dongye1912 2012-10-30 03:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题