Increment hash key (not values)

57 views
Skip to first unread message

George Chilumbu

unread,
Apr 11, 2016, 6:45:38 AM4/11/16
to Redis DB
I am using phpredis and  want to be able to increment my hash key e.g 

key1 username "Joe" title "boss"
key2 username "Mary" title "Real boss"
...

So in phpredis, i did something like the following but does not work as demonstrated above:

<php
define('KEY', 'key'.$counter);
$counter = 0;
while($counter < 0){
    $id = "ID-".$counter;
    $redis->hmset(KEY, array($counter=>$id));
    $counter++;
}
...


The program runs fine with no errors, but is not just working as demonstrated above. So the issue is basically with this line of code:

define('KEY', 'key'.$counter);

I also tried this but same result.

define('KEY', "key".$counter);

Instead, the program is executing this:

HMSET" "key" "0" "ID-0"

HMSET"
"key" "1" "ID-1"

HMSET
" "key" "2" "ID-2"

...

Jan-Erik Rediger

unread,
Apr 11, 2016, 7:00:40 AM4/11/16
to redi...@googlegroups.com
> define('KEY', 'key'.$counter);

This defines you a constant. It is not updated whenever you increment
$counter.
What you actually want to use is a variable.
You should read up on how to do that in PHP.

(This is not an issue with Redis at all, it is just a misuse of your
programming language)
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
> To post to this group, send email to redi...@googlegroups.com.
> Visit this group at https://groups.google.com/group/redis-db.
> For more options, visit https://groups.google.com/d/optout.

George Chilumbu

unread,
Apr 11, 2016, 12:05:45 PM4/11/16
to redi...@googlegroups.com, Jan-Erik Rediger
Indeed i am not a Php programmer, but a DBA. However, i believe that $counter is a php variable, and its global since it is not inside a function or anything else.

So i am not sure what you mean when you say "what you actually want to use is a variable". Please, provide me with an example?

You received this message because you are subscribed to a topic in the Google Groups "Redis DB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/redis-db/swPhU6PRe_I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to redis-db+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages