0

I get an error on line 15 that says "Undefined variable: row2". How can I resolve this?

$limit = 20;

$res1 = mysql_query('SELECT *
                        FROM contact
                        WHERE name = "Greg"');
$res2 = mysql_query('SELECT name
                        FROM contact c, passport p
                        ON c.idNum = p.iNum
                        WHERE date >= "2015-03-03" AND t< "2015-03-21');

if(!$res1 && !$res2) {
    die('Query no valid: ' . mysql_error());
}
else {
    while(($row1 = mysql_fetch_array($res1)) || ($row2 = mysql_fetch_array($res2))) {
        $sub = $row1['num'] - $row2['num'];
        if($sub <= $limit) {
            echo '<br>row name is: ', $row2['name'];
        }
    }
}

What I'm trying to do is get a number from the first table (it only results to just Greg's row). Then subtract it with the numbers from the results of the second table. The result of this is placed into the sub variable and it's check to see if it's <= 20. If yes, it prints out the row. If not, it goes back to while loop to check another row. Am I going about the right way so far?

8
  • 3
    You're subtracting $row2['num'], but only selecting name in your second query Commented Apr 17, 2016 at 23:09
  • 2
    So there is no $row2['num'] as you did not select that column Commented Apr 17, 2016 at 23:10
  • Well..You haven't selected the num column in your second query so that's why it shows as undefined.! Commented Apr 17, 2016 at 23:14
  • 1
    Lazy evaluation means that $row2 willl not exist until all $res1 results have been fetched Commented Apr 17, 2016 at 23:17
  • This could be done using a JOIN statement in MySQL, using the c.idNum Commented Apr 17, 2016 at 23:22

2 Answers 2

2

You need to change the while() loop's condition. Consider this example:

$a = 1;

if ($a == 1 || $b = 2) {
  var_dump(isset($b));
}

Output of var_dump() will be boolean false because $b does not exist, which is the same case why your $row2 is undefined.

The thing is, while evaluation conditions with ||, PHP will stop evaluating other conditions once the match is found, so other comparisons or assignments on the right side will not be performed.

Change your while to be like this, you need both $row1 and $row2 anyway:

while(($row1 = mysql_fetch_array($res1)) && ($row2 = mysql_fetch_array($res2))) {

(note the && instead of ||)

Also, looks like you may want to use SELECT c.* in your second query, too, because you're only selecting the name column, and trying to use num too.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the advise, regarding &&, no error messages anymore. Unfortunately, I'm not retrieving any data. :( So I have to keep working on it.
1

Note : Select all columns in your 2nd Query if num is already available in your columns so your problem will be solved then.!

Note : Try to replace || with && and you will be good to go.

By using || or OR as in conceptional language as I would say it.You are making the code like in a way that either and only one will pass but if you are passing both ones so then you should replace || with && so that's why your $row2 will be already created then so it will be available for more operation.!

$limit = 20;

$res1 = mysql_query('SELECT *
                        FROM contact
                        WHERE name = "Greg"');
$res2 = mysql_query('SELECT *
                        FROM contact c, passport p
                        ON c.idNum = p.iNum
                        WHERE date >= "2015-03-03" AND t< "2015-03-21');

if(!$res1 && !$res2) {
    die('Query no valid: ' . mysql_error());
}
else {
    while(($row1 = mysql_fetch_array($res1)) && ($row2 = mysql_fetch_array($res2))) {
        $sub = $row1['num'] - $row2['num'];
        if($sub <= $limit) {
            echo '<br>row name is: ', $row2['name'];
        }
    }
}

12 Comments

you fixed the query but $row2 will be undefined variable still.
@Umair Shah Yousafzai, I tried it and it still gives me the same error message.
@mrteeth : Try var_dump OR print_r to confirm if you are getting any data from both executed queries.!
@Umair Shah Yousafzai, the error has been resolved. Thanks a lot! However, I'm not getting any results back. There's 200 records in my database and I hoping to get back 50 (I manually checked the ones that fit my criteria). I'll try and fix this.
@mrteeth : Sure!!! No Problem.Try to make it your habit to first check the executed data from your database by using var_dump OR print_r and then move forward with your code.Also mark your answer if it fixed your problem.
|

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.