0

I have the following code

$sqlSubscription = "SELECT `user_id` FROM subscriptions WHERE `user_id` = '".$user_id."' and `course_id` = 'calculus_1'";
            $subscriptionResult = mysql_query($sqlSubscription);

            if($subscriptionResult === FALSE) {
    die(mysql_error()); // temp error handling
              }


                while ($rows = mysql_fetch_assoc($subscriptionResult))
                    {
                        $user_id = $rows['user_id'];
                        $course_id = $rows['course_id'];
                        if($user_id==1 && $course_id="calculus_1")

                            {

                                //do some work


                            }

                     }

I'm getting the error Notice: Undefined index: course_id The line that causes the error is in the while statement $course_id = $rows['course_id']; The strange part is that I can select from where both user and course ids match, and there aren't any errors doing something simple like an echo, but when I add this while statement to verify that both the user and course_ids match a certain rule before outputting data I get this error.

Here is an export of the PHP Arrays through phpMyAdmin

<?php
/**
 * Export to PHP Array plugin for PHPMyAdmin
 * @version 0.2b
 */

//
// Database `test`
//

// `escholars`.`subscriptions`
$subscriptions = array(
  array('user_id' => 'test','course_id' => 'calculus_1','start_date' => '2013-09-12','end_date' => '2013-09-28')
);

Any ideas?

Thanks

4

2 回答 2

2

要进行比较,您需要 == 标志

if($user_id==1 && $course_id=="calculus_1")

而且您正在选择 WHERE course_id="something" 这样您就不需要从数据库 $rows['course_id'] 返回相同的值

$course_id = "calculus_1";
于 2013-09-12T16:43:11.880 回答
1

您似乎没有选择course_id.

您需要选择它才能将其包含在结果集中。否则它将不会包含在您获取的数组中并且将是未定义的。

尝试这个:

SELECT `user_id`,`course_id` FROM .....
于 2013-09-12T16:43:04.457 回答