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?

mrteeth
  • 153
  • 1
  • 1
  • 8
  • 3
    You're subtracting `$row2['num']`, but only selecting `name` in your second query – andrewsi Apr 17 '16 at 23:09
  • 2
    So there is no `$row2['num']` as you did not select that column – RiggsFolly Apr 17 '16 at 23:10
  • Well..You haven't selected the `num` column in your second query so that's why it shows as undefined.! – Umair Shah Apr 17 '16 at 23:14
  • 1
    Lazy evaluation means that $row2 willl not exist until all $res1 results have been fetched – Mark Baker Apr 17 '16 at 23:17
  • This could be done using a `JOIN` statement in MySQL, using the `c.idNum` – Sean Apr 17 '16 at 23:22
  • Okay, I see. Terrible mistake by me! Thanks for pointing that out to me – mrteeth Apr 17 '16 at 23:22
  • @Sean Okay, thanks for advising. What's wrong with ON statement, by the way? – mrteeth Apr 17 '16 at 23:24
  • @mrteeth the `ON` is fine. I realized that I misread your code. I was thinking you could do a `JOIN` with `$res1` & `$res2`. But now that I reread your code, it looks like you are comparing 1 row from `contact` against all rows from `contact c, passport p` in the date range. So nevermind. – Sean Apr 17 '16 at 23:42

2 Answers2

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.

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44
  • thanks for the advise, regarding &&, no error messages anymore. Unfortunately, I'm not retrieving any data. :( So I have to keep working on it. – mrteeth Apr 17 '16 at 23:50
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'];
        }
    }
}
Umair Shah
  • 2,305
  • 2
  • 25
  • 50
  • you fixed the query but `$row2` will be undefined variable still. – Oliver Maksimovic Apr 17 '16 at 23:26
  • @Umair Shah Yousafzai, I tried it and it still gives me the same error message. – mrteeth Apr 17 '16 at 23:28
  • @mrteeth : Try `var_dump` OR `print_r` to confirm if you are getting any data from both executed queries.! – Umair Shah Apr 17 '16 at 23:31
  • @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 Apr 17 '16 at 23:39
  • @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. – Umair Shah Apr 17 '16 at 23:41
  • Also when using print_r, should I use it like this: "print_r($row2);"? – mrteeth Apr 17 '16 at 23:42
  • The same with var_dump, should I use it like this "var_dump($row2);"? Also what line should I place it? – mrteeth Apr 17 '16 at 23:44
  • @mrteeth : Yes..You are using it the right way & Don't hesitate to ask for help anwhere you need. – Umair Shah Apr 17 '16 at 23:44
  • @Umair Shah Yousafzai, what like of my code should I be using `var_dump` and `print_r`? I placed it after the echo statement and it didn't display any data. – mrteeth Apr 17 '16 at 23:47
  • @mrteeth : Use `var_dump` OR `print_r` after fetching data from database through passing the array variable which contains the fetched data which in your case is `$row1` and `$row2` also make your habit to use `while` loop less than other loops as foreach or for loop.foreach and forloops are better to use instead of while. – Umair Shah Apr 17 '16 at 23:47
  • @Umair Shah Yousafzai: Interesting, regarding for/eachloop. I tried to do this and it didn't work. Is it okay if you can give me an example code? – mrteeth Apr 17 '16 at 23:53
  • @mrteeth :Take a look at : http://stackoverflow.com/questions/10326346/how-to-use-multiple-arrays-in-single-foreach-loop – Umair Shah Apr 18 '16 at 00:17