We help IT Professionals succeed at work.

PHP returning \N when dividin by aeron MySql Table

robrodp
robrodp asked
on
309 Views
Last Modified: 2014-08-25
I have a MySql table and I manipulate numbres. I divide a row of numbrs by zero and get no error the result is just \N. It  I really need to 0's in the result.
Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Dividing by zero is 'undefined' and does not return 0's.  More info here: http://dev.mysql.com/doc/refman/5.0/en/precision-math-expressions.html
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
You cannot divide by zero in PHP - why on earth would you want to?
Enable error reporting in your page and you will see the error.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
robrodpProgrammer

Author

Commented:
Hey guys.. I knew that you cannot divide by zero.

My table has colums, for each comul y sum its total and didife each cell by this total and I get a participationcpercentage.

When the colum sums zero (lno sales or whatever) I amgetting this result I was expecting an error, but no error just this \N
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
The MySQL response is listed in the link above.  Default response is blank like ''.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Please show us the table structure, a bit of test data, and the exact query you're using so we can load the tables and duplicate the problem.  This is called the SSCCE and it's essential if you want a quick and accurate (eg: tested) answer.
robrodpProgrammer

Author

Commented:
the table
is:

 for simpliciyi I have ta bele with 1 column and n rows...

Say
Sales
                 Year1    Year2
Total         1,600      0
Auto        1,000       0
Clothing    500       0
Food          100       0
Car                0       0

I need percenteges

                 Year1    Year2
Total         1,600      0
Auto        1,000       0
Clothing    500       0
Food          100       0
Car                0       0

I need a table with:

Year 1      Year 2
62.5         0
50              0
20             0
0             0

I sum the values of teh columa and then divide. Not too hard... but rather complex sytax

But for year 2 I just cant figure it out properly.


Thx
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Are you doing this in the sql or the php, somewhere up above (cannot see it now) Ray gave you the IF NULL clause you can use in an SQL statement
robrodpProgrammer

Author

Commented:
I doing it in MySql

Like

cast(colname/(select sum(colname) from file where not colname='Total')  *  100 as decimal(5,2))
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
robrodpProgrammer

Author

Commented:
That did the trick

Thx guys,,,, and I know that dividing by zero isa no no in math....

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.