PHP returning \N when dividin by aeron MySql Table

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.
robrodpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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
0
GaryCommented:
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.
0
Ray PaseurCommented:
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/
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

robrodpAuthor 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
0
Dave BaldwinFixer of ProblemsCommented:
The MySQL response is listed in the link above.  Default response is blank like ''.
0
Ray PaseurCommented:
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.
0
robrodpAuthor 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
0
GaryCommented:
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
0
robrodpAuthor Commented:
I doing it in MySql

Like

cast(colname/(select sum(colname) from file where not colname='Total')  *  100 as decimal(5,2))
0
GaryCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robrodpAuthor Commented:
That did the trick

Thx guys,,,, and I know that dividing by zero isa no no in math....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.