doutui9606 2018-01-07 20:22
浏览 33

php中的db(sql)数据的数字检查和操作不起作用

I have a SQL database that includes a column called TotaleFattura where I store the total amount of an invoice, in the format "xxx,xx" (so with comma, in italy this is the separator for decimals) In the php script I try to take this value and if it's bigger than 77,47 I have to display a string via echo command. If it's smaller, nothing has to happen and the string must be empty.

Issue is that the operation not works! I means that the condition is always FALSE also when it shoudl be TRUE (and in some cases it is always TRUE also when it should be FALSE, please keep reading you will understand).

So happens that also when the db field is bigger than 77,47 the strings not shows. Basically it seems that the "if" statement is unable to compare correctly the two parameters/variable like they are a string and a number that cannot be compared, but from what I know PHP will manage the conversion itself. Or I miss something?

Anyway, this is my code

$totfattura = $row['TotaleFattura'];

echo "Totale Fattura: " . $totfattura . "<br />";

if ($totfattura > "77,47") {
    $notefattura = "this is my note. invoice is bigger than 77,47";
    }

echo "Note Fattura: " . $notefattura . "<br />";

I made the SQL table using the "import" from a CSV file directly in the PhpMyAdmin panel from my hosting provider. I tried to have the import of the data in two different ways: decimal(5,2) or varchar(8) utf8_general_ci but nothing changed about the result.

So basically, to have a recap:

If I use

$totfattura > "77,47"

the result is ALWAYS TRUE (also when $totfattura is smaller than 77,47)

Then, If I use

$totfattura > "77.47"

the result is ALWAYS FALSE (also when $totfattura is bigger than 77,47)

I'm sure I'm missing something really simple, but can't figure it out. Hope someone will help me. Thanks!

p.s. If you need any other data or a db extraction please ask and I will provide you all the details.

  • 写回答

1条回答 默认 最新

  • dongqiu3709 2018-01-07 22:34
    关注

    @GraemeChapman you made it. it seems to work with if (floatval($totfattura) > 77.47)

    I'm feeling so stupid, the fact is that looking at php docs, it says that the during a comparison between two variables them will be automatically converted to the same type. Look here -> http://php.net/manual/en/language.operators.comparison.php

    It says "If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. "

    So this is weird, or I'm missing something? Maybe I'm missing the meanings of the word "string" ???

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀