bidntrade Posted July 4, 2009 Share Posted July 4, 2009 i have this code .... it does not seem to work ..... I would like to test : product_id and weight when updating to make sure they match my variable . and i would like to test to see if the price has changed . $price = round($price, 2); $query = 'SELECT cscart_products.product_id FROM cscart_products LEFT JOIN cscart_product_prices USING (product_id) WHERE product_id = ' . $id . ' AND weight = ' . $weight . ' AND price != ' . $price; if ($result = mysql_query($query)) { if($go){ // entry exists update if(mysql_query("UPDATE cscart_product_prices PP LEFT JOIN cscart_products PI USING (product_id) SET PP.price = '$price', PI.amount = '$qty' WHERE PI.product_code = '$id' AND PI.weight = '$weight'")){ $emailinfo .= "Product ".$id." Price Updated To ".$price. " Stock= " . $qty . " .\n"; echo "Product ".$id." Price Updated To ".$price. " Stock= " . $qty . " .<br>"; } } } $result = ""; $go='1'; } go easy on me im very new to php ... as you can tell from the code ... the price variable sometime reads like this 9.5 but my database stores them like 9.50 i would rather not have to do the select and just test if the update matched anything or not with a WHERE clause . but for some reason even if its not there it returns true Note: the code im using this in is going through a csv file one line at a time nightly checking for price changes . there is about 3600 products in the csv. HELP ! Quote Link to comment https://forums.phpfreaks.com/topic/164722-how-do-i-test-if-update-was-needed-and-perform-action-if-update-happened/ Share on other sites More sharing options...
fenway Posted July 4, 2009 Share Posted July 4, 2009 You need to check the rows affected.... Quote Link to comment https://forums.phpfreaks.com/topic/164722-how-do-i-test-if-update-was-needed-and-perform-action-if-update-happened/#findComment-868633 Share on other sites More sharing options...
bidntrade Posted July 4, 2009 Author Share Posted July 4, 2009 any clue for a newbie ? Quote Link to comment https://forums.phpfreaks.com/topic/164722-how-do-i-test-if-update-was-needed-and-perform-action-if-update-happened/#findComment-868715 Share on other sites More sharing options...
trq Posted July 4, 2009 Share Posted July 4, 2009 i would rather not have to do the select and just test if the update matched anything or not with a WHERE clause . but for some reason even if its not there it returns true This is because mysql_query returns trueor false depending on wether or not the query succeeded or not, not if it found any results. There is a difference. You need to check if any results where found in your select query. eg; $sql = "SELECT * FROM foo WHERE id = 1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // match found (do update). } else { // no match found (do insert). } } You should always check the number of rows found with select queries before attempting to use the result. Quote Link to comment https://forums.phpfreaks.com/topic/164722-how-do-i-test-if-update-was-needed-and-perform-action-if-update-happened/#findComment-868722 Share on other sites More sharing options...
fenway Posted July 4, 2009 Share Posted July 4, 2009 Yes, but in this case, it's an update... so you simply need to use mysql_affected_rows(). Quote Link to comment https://forums.phpfreaks.com/topic/164722-how-do-i-test-if-update-was-needed-and-perform-action-if-update-happened/#findComment-868946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.