-2

In a couple scripts which I've created I get the same error when I connect on my wampserver.

Every thing is running smooth but I don't like those errors. Notice: Undefined variable

the $watdan function is giving me this error.

For example I've to amounts that need to be count up: 2835 + 11024 I've already multipled those. Now when I use this code:

            $total[0] = $totaal->amount;
            $total[1] = $totaal->price;
            $watdan += $total[0] * $total[1];

Im getting the right answer 13859 but I'm also getting the error which I've mentioned.

Now when I add $watdan=0; I only get the 11024 without the given error.

this is the full code:

    $pak14 = $db->query("SELECT * FROM log_drops INNER JOIN log_items ON log_drops.item = log_items.name AND log_drops.game = log_items.game WHERE log_drops.log_id = '".$id."' ORDER BY log_drops.log_name ASC") or die($db->error);
    while($totaal = $pak14->fetch_object())
    {

            $total[0] = $totaal->amount;
            $total[1] = $totaal->price;
            $watdan += $total[0] * $total[1];
        } 
    if(isset($watdan))
    {
        echo $watdan; echo ' Gold';
    }

3 Answers3

2

you have to initialize this variable to 0(zero first)

            $watdan=0;
            $total[0] = $totaal->amount;
            $total[1] = $totaal->price;
            $watdan += $total[0] * $total[1];
vaibhav kulkarni
  • 1,733
  • 14
  • 20
1

Not Declare the variable $watdan. Declare variable with 0 or '' after use it.

$watdan=0;//add this in your code
$total[0] = $totaal->amount;
$total[1] = $totaal->price;
$watdan += $total[0] * $total[1];

check this

 $pak14 = $db->query("SELECT * FROM log_drops INNER JOIN log_items ON log_drops.item = log_items.name AND log_drops.game = log_items.game WHERE log_drops.log_id = '".$id."' ORDER BY log_drops.log_name ASC") or die($db->error);
$watdan=0;//add this in your code
while($totaal = $pak14->fetch_object())
{

        $total[0] = $totaal->amount;
        $total[1] = $totaal->price;
        $watdan += $total[0] * $total[1];
    } 
if(isset($watdan))
{
    echo $watdan; echo ' Gold';
}
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
1

Declare first your $watdan variable as:

 $watdan = 0;

Then it will work. You can not directly assign value to variable without declare it with short hand operators.

Yash
  • 1,446
  • 1
  • 13
  • 26