Question

How could I eliminate this error

ERROR: division by zero

I already put if statement but still not working. Any ideas?

In my Model page

$row = $this->db->query("SELECT column1 from table where id=1");

   if ($row->num_rows() >0){

           $this->db->query("UPDATE table2 SET tech_voc=
           (select column1 from table where id=2 )/
           (select column1 from table where id=1 )
       WHERE id=2;");}

I put if statement because sometimes the value of $row is 0. Is there anyway I can run this code?

Was it helpful?

Solution 2

You only check whether records exists before the if condition. Also you need to check if the value is greater than 0.

So the complete code can be written like this without the if condition in PHP.

       $this->db->query("UPDATE table2 SET tech_voc=
       (select column1 from table where id=2 )/
       (select column1 from table where id=1 )
   WHERE id=2 AND exists (SELECT column1 from table where id=1 and column1 >0 );");}

OTHER TIPS

Division-by-zero errors happen when the divisor is 0. You're checking the wrong number.

The bottom half (where id=2) is zero, and you're checking the top half (where id=1). You also only appear to be checking for the existence of the row, rather than checking whether the value is 0.

It's mysql error.

You should check value of column1 (it's seems that it has value 0).

$row = $this->db->query("SELECT column1 from table where id=1");
$row2 = $this->db->query("SELECT column1 from table where id=2");

   if ($row->num_rows() >0 && $row2->fetchColumn()!=0){

           $this->db->query("UPDATE table2 SET tech_voc=
           (select column1 from table where id=1 )/
           (select column1 from table where id=2 )
       WHERE id=2;");}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top