UPDATE cab_details SET travel_company_person_name='$user_role', driver_name='$driver_name' , driver_phone_no='$driver_phone' , vehicle_name='$vehicle_name' , vehicle_no='$vehicle_no' , starting_kms = '$starting_kms' , region='$region', cd_comments='$comments' WHERE travel_id='$id' AND ((travel_id NOT IN (SELECT cancel_customer.ctravel_id FROM cancel_customer WHERE ctravel_id='$id')) AND travel_id NOT IN (SELECT tcc_travel_id FROM travel_company_cancel WHERE tcc_travel_id='$id' AND tcc_travel_cancel_company='$travel_company'))
在上面的sql查询中,当数据与现有数据匹配时,mysql_affected_rows()返回0,并且当条件不满足时返回0。 我想提醒用户"数据没有变化"或"条件不满意"。
我该怎么做?
答案 0 :(得分:2)
来自文档:
使用UPDATE时,MySQL不会更新新值的列 与旧值相同。这创造了这样的可能性 mysql_affected_rows()实际上可能不等于行数 匹配,只有受字面意义影响的行数 查询。
由于无法根据mysql_affected_rows()的返回值进行区分,因此可以运行查询以测试记录是否首先存在于查询的WHERE条件中。
SELECT count(*) as count FROM cab_details
WHERE travel_id='$id' AND
((travel_id NOT IN (SELECT cancel_customer.ctravel_id FROM cancel_customer WHERE ctravel_id='$id')) AND
travel_id NOT IN (SELECT tcc_travel_id FROM travel_company_cancel WHERE tcc_travel_id='$id' AND tcc_travel_cancel_company='$travel_company'))
如果返回的行数为0,则返回“条件不满足”
如果行数> 0,对mysql_affected_rows()进行更新和测试,如果= 0,则返回“数据没有变化”