HTTP request in loop()

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  delay(1000);
  
}
void loop() {
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (client.connect(server, 80)) {
      Serial.println("connected");
      // Make a HTTP request:
      client.println("GET /2arduino.php HTTP/1.1");
      client.println("Host: server");
      client.println("Connection: close");
      client.println();
    }
    if (client.available()) {
      String c = client.readString();//raw php return    
      String value = c.substring((c.indexOf('*'))+1, c.indexOf('*', (c.indexOf('*'))+1)); //query string
      String v1;
      long int v2;
   
      for (int i = 0; i < value.length(); i++) { //splits and assigns variables.
        if (value.substring(i, i+1) == "~") {
          v1 = value.substring(0, i);
          v2 = value.substring(i+1).toInt();
          break;
        }
      }
      
      Serial.println(v1);
      Serial.println(v2);
    }
  }
}

I am using the ethernet shield to connect to my wamp server. I am trying to execute the query, get the result, split the result string and save them to variables v1 and v2.

Once i run the program, it makes the http request and gives me the results. That's perfect! HOWEVER, it only does it ONE time. The idea is that it executes the php with the interval rate of 5000ms. What would be the problem here? i cant figure it out.

With only a small part of the sketch, we have to guess what the rest of the code is.

Is the previousMillis an unsigned long ? Is the "server" text valid ? and it is not a PROGMEM string ? Did you compare your sketch with this Ethernet - Arduino Reference ? You don't print the "server" string, you print the word "server". Do you use an Arduino Uno, and the ram is too small ? Where is the client.stop() ? Did you test the code with the Strings in a separate test sketch using the Serial monitor ?

my appologies the top part of the code is:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 192, 168, 1, 2 }; // WAMP SERVER
IPAddress ip(192,168,1,3); //arduino IP
EthernetClient client;

unsigned long previousMillis = 0;
long interval = 5000;

yes when i put the word "server" it is valid. I dont think RAM was the issue here because it is a very light sketch. I did make sure that all the variables were declared the proper way.

Anyways after trial and error i finally got it to constantly make the HTTP request. It would seem that i get very confused making validations using the client.connect/client.available.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 192, 168, 1, 2 }; // WAMP SERVER
IPAddress ip(192,168,1,3); //arduino IP
EthernetClient client;
unsigned long previousMillis = 0;
long interval = 5000;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  delay(1000);
  
  if (client.connect(server, 80)) {
    Serial.println("connected");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop(){
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

  if (client.connected()==true) {
  
    client.println("GET /2arduino.php HTTP/1.1");
    client.println("Host: server");
    client.println(); 
    delay(2000);
    if (client.available()) {
      String c = client.readString();//raw php return    
      String value = c.substring((c.indexOf('*'))+1, c.indexOf('*', (c.indexOf('*'))+1)); //desired string
      String v1;
      long int v2;
   
      for (int i = 0; i < value.length(); i++) { //splits desired string and assigns variables.
        if (value.substring(i, i+1) == "~") {
          v1 = value.substring(0, i);
          v2 = value.substring(i+1).toInt();
          break;
        }
      }
      
      Serial.println(v1);
      Serial.println(v2);
    }
  }
}
}

Some delay between the GET and the the client.available() is okay. Two seconds is a long time for a local server. Perhaps 100ms is enough.

You have moved the client.connect() into the setup(), so the client.connect() is executed just once. There is however still no client.stop().

The difference between connected() and available() is:
The client.connect() opens a socket to a server. The client.connected() checks if the socket is still open, since the server could close it. The client.available() checks for incoming data.

To get data, the client.available() should be checked if there is any data. But also the client.connected() should be checked, since the server might have closed the connection. Some use both checks for every single byte that is read, but I don't do that. You use the readString to read data with a timeout. That's is okay. I use a timeout in my sketch, similar to the 'loopcount' in this example : Arduino Playground - WebServerST