PHP returning \N when dividin by aeron MySql Table

robrodp
robrodp used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
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
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
Top Expert 2016
Commented:
You cannot divide by zero in PHP
You cannot divide by zero in mathematics!  My guess is that the \N is an indicator of Not-a-Number.  Your script will need to test for zeros and avoid dividing by zero.

See if this helps: http://www.mysqltutorial.org/mysql-nullif/
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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
Most Valuable Expert 2014

Commented:
The MySQL response is listed in the link above.  Default response is blank like ''.
Most Valuable Expert 2011
Top Expert 2016

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
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))
Expert of the Year 2014
Top Expert 2014
Commented:
Actuall ifnull wouldn't work for you
Untested:

cast(colname/(select CASE WHEN sum(colname)=0 THEN 1 else sum(colname) END from file where not colname='Total')  *  100 as decimal(5,2)) 

Open in new window

...and probably wrong...always find this confusing in MySQL
robrodpProgrammer

Author

Commented:
That did the trick

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial