0

Is it possible to somehow calc the difference between two rows of one table?

I tried this, but it doesnt work:

$diff = mysql_num_rows("
SELECT (`used`-`paid`) 
FROM `coupons_codes` 
WHERE `cid`='".$data['cid']."'");       
Vay
  • 117
  • 2
  • 14
  • please elaborate on "it doesnt work": what was the problem/error? Also, it is important to know the data type of `used` and `paid` columns. – bpgergo Jan 12 '12 at 13:18
  • is `used` and `paid` of type numerical? if `used-paid` is a valid syntax. It'll subtract it. – Shiplu Mokaddim Jan 12 '12 at 13:18
  • Oh, I see. No it's boolean to check if a coupon is paid or used (redeemed). Is it possible to solve this anyway with one query? I need the difference of the sum of paid and used. – Vay Jan 12 '12 at 13:25

1 Answers1

0

You should do something like this:

//store the query in a string
$query = "SELECT `used`-`paid` FROM `coupons_codes` WHERE `cid`='".$data['cid']."'";
//execute the query on the MySQL database
$result = mysql_query($query);
//extract the result from the response from the MySQL server
$diff = mysql_result($result, 0, 0);

The function you're using is used to count the number of rows in a resultset obtained from a mysql_query(); call.

I suggest you try to read up on some basics concerning PHP and MySQL, I have a feeling that you lack some basic knowledge to really create good code.

user254875486
  • 11,190
  • 7
  • 36
  • 65
  • Well, I had two queries like `$paid = mysql_num_rows ... WHERE paid='1'` and `$used = mysql_num_rows ... WHERE used='1'` to get the sum of paid and used to substract it in php via $diff = $used-$paid , because paid and used is boolean and not a nummeric type. So I just realized that's why it won't work. But isn't there a better solution instead of two mysql_num_rows-queries to get the sum of all paid and used entries and finally to substract them? – Vay Jan 12 '12 at 13:32