Check if it is a valid account

Asked

Viewed 58 times

0

I’m making a Mysql query in which one of the columns would be the result of a calculation.

However, I need to check in PHP if this account is valid. For example, Pow(-0.2,0.5) would be an impossible value because it would be the same as trying to take the square root from a negative value. I have a problem too that in this field does not necessarily need to be an account, it may only come as the name of some table column.
I thought about using Eval(), but it gives error for every time I do not enter an account but the name of a column.
Is there any way to validate if it’s a possible calculation and, if it’s any name, do nothing?

EX: select Pow(-0.2,0.5),columnA,colunaB from Tabela1 Where name="John" would result in error since the first account is impossible. But it may come in the following format: select Pow(colunaA,colunaB),colunaA,colunaB from Tabela1 Where name="John" and in that case, it would not be necessary to make a check if it is a valid account.

  • 1

    Can you show a little code? would be easier to understand the question..

  • could check with an is_numeric the result, but then, even though it is a enorm fraction, it would be pointed out as true.

  • Using is_numeric I believe it will not work pq using is_numeric(Eval('Return'.$account.';')) it holds true even though it is an impossible account

1 answer

0

Do you want to solve the problem by php or mysql? Also, I believe that there is no what you want, you will probably have to make a series of validations.

mysql:

select if(colA >= 0, pow(colA,ColB), 'NAN') from tabelaA where nome = 'joao';

If it is in php it is simpler because the calculation you sent (Pow(-0.2,0.5)) will not give an error, it will return you NAN. In other cases as for example take the root of a number is just check if it is not < 0. To solve the column problem do not return a number can use is_numeric or make a cast

  • I want to solve by PHP so that it arrives in Mysql in a valid way. I think my main problem is when I try to do an Eval of non-numerical things, for example Eval('Return Pow(columnA,colunaB);') which gives syntax error.

  • If this is the case, validations must be done manually, for example: if ($colunaA >= 0) { Eval('Return Pow($colunaA, $colunaB)'; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.