@@ -38,8 +38,8 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
3838		return  -1 ;
3939
4040	eof  =  php_stream_eof (redis_sock -> stream );
41-     while ( eof ) {
42-         if ((MULTI  ==  redis_sock -> mode ) ||  redis_sock -> watching  ||  count ++  ==  10 ) { /* too many failures */ 
41+     for  (;  eof ;  count ++ ) {
42+         if ((MULTI  ==  redis_sock -> mode ) ||  redis_sock -> watching  ||  count  ==  10 ) { /* too many failures */ 
4343	    if (redis_sock -> stream ) { /* close stream if still here */ 
4444                php_stream_close (redis_sock -> stream );
4545                redis_sock -> stream  =  NULL ;
@@ -61,6 +61,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
6161            eof  =  php_stream_eof (redis_sock -> stream );
6262        }
6363    }
64+ 
65+     // Reselect the DB. 
66+     if  (count  &&  redis_sock -> dbNumber ) {
67+         char  * cmd , * response ;
68+         int  cmd_len , response_len ;
69+ 
70+         cmd_len  =  redis_cmd_format_static (& cmd , "SELECT" , "d" , redis_sock -> dbNumber );
71+ 
72+         if  (redis_sock_write (redis_sock , cmd , cmd_len  TSRMLS_CC ) <  0 ) {
73+             efree (cmd );
74+             return  -1 ;
75+         }
76+         efree (cmd );
77+ 
78+         if  ((response  =  redis_sock_read (redis_sock , & response_len  TSRMLS_CC )) ==  NULL ) {
79+             return  -1 ;
80+         }
81+ 
82+         if  (strncmp (response , "+OK" , 3 )) {
83+             efree (response );
84+             return  -1 ;
85+         }
86+         efree (response );
87+     }
88+ 
6489    return  0 ;
6590}
6691
@@ -794,6 +819,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
794819    redis_sock -> stream  =  NULL ;
795820    redis_sock -> status  =  REDIS_SOCK_STATUS_DISCONNECTED ;
796821    redis_sock -> watching  =  0 ;
822+     redis_sock -> dbNumber  =  0 ;
797823
798824    redis_sock -> persistent  =  persistent ;
799825
@@ -933,6 +959,7 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
933959	    return  1 ;
934960    }
935961
962+ 	redis_sock -> dbNumber  =  0 ;
936963    if  (redis_sock -> stream  !=  NULL ) {
937964			if  (!redis_sock -> persistent ) {
938965				redis_sock_write (redis_sock , "QUIT" , sizeof ("QUIT" ) -  1  TSRMLS_CC );
0 commit comments