-4

i get this php error

Notice: Undefined variable: total_earn in /var/www/..../hello.php on line 221

But the variable works fine.. anyone can help me & have an idea ?

<?php
              $con=mysqli_connect("localhost:3306","***","***","superpayment");
              $a="select * from users where ref='".$req_user_info['refer']."' ";
              $b=mysqli_query($con,$a);
              $c=mysqli_fetch_array($b);
                $mm="select * from configuration where config_name='refer_referer_points' ";
                $nn=mysqli_query($con,$mm);
                $oo=mysqli_fetch_array($nn);


              if($c['id']>0) {
                  $x="select * from users where ref='".$req_user_info['refer']."' ";
                  $q=mysqli_query($con,$x);

              $i=0;
              while($res=mysqli_fetch_array($q))
              {
                  $i++;

                 $x="select count(*) as ld, sum(points_used) as earn from Completed where username='".$res['username']."' ";
                  $y=mysqli_query($con,$x);

                  $z=mysqli_fetch_array($y);

                  $user_earn=$z['earn']/1000;
                  $your_earn=($user_earn*$oo['config_value'])/100;
                  $total_earn=$total_earn+$your_earn;
              ?>
Eddie
  • 26,593
  • 6
  • 36
  • 58
Veltins
  • 55
  • 2
  • 8

1 Answers1

1

The jist of it, is $total_earn is not yet initialized when you're doing $total_earn=$total_earn+$your_earn;. While PHP will automatically initialize it (As 0), it's letting you know that by not initializing it, it could be a bug in your code.

It's helpful when you may accidentally misspell a variable. See this post for more information on this.

Set it like so:

$i=0;
$total_earn=0;
while($res=mysqli_fetch_array($q))
{
    $i++;
    $x="select count(*) as ld, sum(points_used) as earn from Completed where username='".$res['username']."' ";
    $y=mysqli_query($con,$x);

    $z=mysqli_fetch_array($y);

    $user_earn=$z['earn']/1000;
    $your_earn=($user_earn*$oo['config_value'])/100;
    $total_earn=$total_earn+$your_earn;
Blue
  • 22,608
  • 7
  • 62
  • 92