Deviant Logo

PHP SQL Subtraction in Query?

post details top
Jun 7th, 2010
post details top


How would I do this? I’m just trying to do a basic subtraction in my SQL Syntax. Need to subtract a PHP held value. Should be easy, but my syntax is off. Help!

$valueB = 5;

$sql = mysql_query(“UPDATE values SET valueA = valueA – ‘$valueB’”);

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

3 Comments

  • catbertnc

    Try setting valueA to a variable, and doing the math in code. Then write the result to the database.

  • General Cucombre

    Remove the quotes around $valueB in your query. If you’re doing math on numbers, you don’t need them:


    mysql_query(“UPDATE values SET valueA = valueA – $valueB”);

    useful tip – if $value comes from elsewhere and you’re not sure if it is numeric or non-empty, do this before query:

    $valueB += 0;

  • 404

    if there is only 1 value in the sql table and if you dont escape from sytnax errors with anyway.
    usethis code

    $valueB = 5;
    $valueA = mysql_query (“select valueA from values”);
    $valueA = mysql_result($valueA,”valueA”,0);
    $valueA = $valueA – $valueB;
    $sql = mysql_query(“UPDATE values SET valueA = valueA – ‘$valueB’”);

Leave a Reply

Search

  • Partner links