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

1

2 Answers 2

2

To compare you need == signs

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

And you are selecting something WHERE course_id="something" so you don't need to return the same value from database $rows['course_id']

$course_id = "calculus_1";
Sign up to request clarification or add additional context in comments.

2 Comments

in the case that i need to verify that two values are met, for example user_id = test and course_id = calculus, how would i setup that if statement using the while loop that i have?
That's a good point, Sbml. The course_id is hardcoded in the query. So, in this context, no need to fetch it back out again.
1

You do not appear to be selecting the course_id.

You'll need to select it in order to include it in the result set. Otherwise it will not be included in your fetched array and will be undefined.

Try this:

SELECT `user_id`,`course_id` FROM .....

1 Comment

To add on, you can always either define the var before doing the while loop, or turn down the error reporting so it won't show these notices, but making it part of the SQL will be a more permanent fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.