0

I have a table "api_keys" where i am storing some API Keys.

The api_keys Table has 5 columns: user, key, api1_limit, api2_limit, api3_limit.

I will be using the last 3 columns to store the sum of each API request. i want to do this because there is an API Limit, so i want to make sure to not pass the limit.

So the idea is to add values to "api limits" columns each time i use the API.

By the way, i am using PDO.

I tried to use the default UPDATE syntax to add to values to the existing values.

The issue is that the 'api1_limit' is the only column that get updated.

$sql_query = "UPDATE api_keys 
            SET api1_limit = api1_limit + :api1_limit_counter,  api2_limit =  api2_limit + :api2_limit_Counter, api3_limit =  api3_limit + :api3_limit_Counter WHERE key = :thekey";


$statement = $connection->prepare($sql_query);
$result = $statement->execute(
    array(
        ':api1_limit_counter'      =>  $api1_limit_counter,
        ':api2_limit_Counter'       =>   $api2_limit_Counter,
        ':api3_limit_Counter'       =>   $api3_limit_Counter,
        ':thekey'                  => $thekey

         )
);

I want to update values in all the 3 columns using 1 call.

9
  • cant you just say api1_limit = api1_limit + 1 etc? Commented Aug 8, 2019 at 13:04
  • @delboy1978uk unfortunately i can't as the api1_limit_counter variable contains the total of how many times i used the api. (i am using foreach to call the api multiple times). Commented Aug 8, 2019 at 13:08
  • are you sure those other two vars have a Capital C? Commented Aug 8, 2019 at 13:21
  • Thank you for catching @Dharman, that wasn't the issue Commented Aug 8, 2019 at 13:22
  • 1
    I just found what was the issue, the "api limits" columns are set to NULL. you can not add to the value if the initial value is NULL, so i start setting the values to 0, now everything works fine. Thank you everybody! Commented Aug 8, 2019 at 13:35

1 Answer 1

1

I found what was the issue, the "api limits" columns are set to NULL. you can not add to the value if the initial value is NULL, so i start setting the values to 0 when i am adding credentials to the api_keys table, now everything works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.