duanri1985 2015-01-04 13:37
浏览 54

php从mysql访问数据时的奇怪查询

Preface

After i executed the query through mysql_query() in php

"update trade set amount=amount-100 where id=123"

If the previous value of amount was 101,after minus 100,the value is supposed to be 1. But i checked the data in different ways:

1. Directly query in mysql:

it returns with 0.780009989(some figure like this,the "0.78" part is actual,after that is just a sample from me,i can't remember exatcly,but i think it doesn't matter)

2.Using php:

while using mysql_fetch_object() in php,it still returns the correct answer of "1".

Test

I've run a test of execute the query 100 times in a loop,but the "error" does not always occur,just sometimes.

Question:

Is it possibly casued by undefined length and decimal length of the "amount" cloumn? Why php funciton can get it correctly? Pls tell me whether something went wrong there.

The detail info of the table and the column as below:

CREATE TABLE tradeid int(11) NOT NULL AUTO_INCREMENT, amountdouble NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=123 DEFAULT CHARSET=utf8;

  • 写回答

2条回答

  • dtkz3186 2015-01-04 13:47
    关注

    Yes, it is caused by the double type of amount field, and if you need precision use DECIMAL. Change the type and see if you get what you want:

    ALTER TABLE trade MODIFY amount DECIMAL(10,2);
    

    That will store 10 digit numbers (8 before decimal point and 2 after). Change this to reflect the data you will store in your DB.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?