user42325: I'm using XAMPP(version-7.2.1) server in my PC to publish my arduino sensor data in a webpage. I have tried the code above and it shows an error like:

Ethernet configured via DHCP
Connecting...
-->connection failed

I have also followed the steps given in this https://forum.arduino.cc/index.php?topic=40448.0 but it doesn't work. 
The code runs till client.connect() after that it doesn't get inside the if(client.connect(server,80)) loop.
The arduino's ethernet shield is assigned an IP by the PC via ICS.
I believe that the problem is in connecting the arduino to the localhost but I don't know how to resolve it. The XAMPP server seems to run properly. I have checked it logs.
I have also posted at https://forum.arduino.cc/index.php?topic=525947.0.
Hope someone can help me
Thanks in advance

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
DHT dht;
const int sensor_pin = A1;
unsigned int ldr;
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 33);
char server[] = "192.168.1.7"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
EthernetClient client;

void setup() {
  Serial.begin(9600); // Serial.begin starts the serial connection between
  computer and Arduino
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet configured via DHCP");
  delay(1000);
  dht.setup(2);
}

void loop() {
  delay(dht.getMinimumSamplingPeriod());  /* Delay of amount equal to sampling period */
  float soilmoisture;
  int sensor_analog;
  sensor_analog = analogRead(sensor_pin);
  soilmoisture = ( 100 - ( (sensor_analog / 1023.00) * 100 ) );
  float temperature = dht.getTemperature();
  float humidity = dht.getHumidity();
  ldr = analogRead(A0);

  Serial.println("Connecting...");
  // Connect to the server (your computer or web page)
  if (client.connect(server, 80)) {
    Serial.println("--> Connected");
    client.print("GET /write_data.php?"); // This
    client.print("temperature="); // This
    client.print(temperature); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print("&humidity="); // This
    client.print(humidity); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print("&soilmoisture="); // This
    client.print(soilmoisture); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.1.7"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println( "Content-Type: application/x-www-form-urlencoded" );
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
  }
  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
  client.flush();
  client.stop();
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
}

My PHP code is as follows:
<?php
// Prepare variables for database connection
$dbusername = "x";  // enter database username, I used "arduino" in step 2.2
$dbpassword = "y";  // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
$dbname = "arduinotest1";
// Connect to your database
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword,$dbname);
if (!$dbconnect) {
    die("Database connection failed: " . mysqli_connect_error());
}
$dbselect = mysqli_select_db($dbconnect, "arduinotest1");
if (!$dbselect) {
    die("Database selection failed: " . mysqli_connect_error());
}
// Prepare the SQL statement
$sql = "INSERT INTO arduinotest1.tabletest2 (temperature,humidity,soilmoisture) VALUES ('".$_GET["temperature"]."','".$_GET["humidity"]."','".$_GET["soilmoisture"]."')";    
// Execute SQL statement
mysqli_query($dbconnect,$sql);
?>


user31195: As far as I can see, when you call client.connect(server, 80), you call it with char[] variable that holds char server[] = "192.168.1.7". And from library code it seems that it will call function with those parameters which will try to resolve dns but can't do it and returns 0.
It calls: int EthernetClient::connect(const char * host, uint16_t port)
,when it should call: int EthernetClient::connect(IPAddress ip, uint16_t port)
Source code: https://github.com/arduino-libraries/Ethernet/blob/master/src/EthernetClient.cpp
So you need to change char server[] = "192.168.1.7"; to IPAddress server(192,168,1,7);
That way it will call the right connection -function and wont get stuck with dns.
Edit: added details

42325: <p>I'm using XAMPP(version-7.2.1) server in my PC to publish my arduino sensor data in a webpage. I have tried the code above and it shows an error like:</p>

<blockquote>
  <p>Ethernet configured via DHCP</p>
  
  <p>Connecting...</p>
  
  <p>-->connection failed</p>
</blockquote>

<p>I have also followed the steps given in this <a href="https://forum.arduino.cc/index.php?topic=40448.0" rel="nofollow noreferrer">https://forum.arduino.cc/index.php?topic=40448.0</a> but it doesn't work. 
The code runs till client.connect() after that it doesn't get inside the if(client.connect(server,80)) loop.</p>

<p>The arduino's ethernet shield is assigned an IP by the PC via ICS.
I believe that the problem is in connecting the arduino to the localhost but I don't know how to resolve it. The XAMPP server seems to run properly. I have checked it logs.</p>

<p>I have also posted at <a href="https://forum.arduino.cc/index.php?topic=525947.0" rel="nofollow noreferrer">https://forum.arduino.cc/index.php?topic=525947.0</a>.</p>

<p>Hope someone can help me</p>

<p>Thanks in advance
</p>

<pre><code>#include &lt;SPI.h&gt;
#include &lt;Ethernet.h&gt;
#include "DHT.h"
DHT dht;
const int sensor_pin = A1;
unsigned int ldr;
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 33);
char server[] = "192.168.1.7"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
EthernetClient client;

void setup() {
  Serial.begin(9600); // Serial.begin starts the serial connection between
  computer and Arduino
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet configured via DHCP");
  delay(1000);
  dht.setup(2);
}

void loop() {
  delay(dht.getMinimumSamplingPeriod());  /* Delay of amount equal to sampling period */
  float soilmoisture;
  int sensor_analog;
  sensor_analog = analogRead(sensor_pin);
  soilmoisture = ( 100 - ( (sensor_analog / 1023.00) * 100 ) );
  float temperature = dht.getTemperature();
  float humidity = dht.getHumidity();
  ldr = analogRead(A0);

  Serial.println("Connecting...");
  // Connect to the server (your computer or web page)
  if (client.connect(server, 80)) {
    Serial.println("--&gt; Connected");
    client.print("GET /write_data.php?"); // This
    client.print("temperature="); // This
    client.print(temperature); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print("&amp;humidity="); // This
    client.print(humidity); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print("&amp;soilmoisture="); // This
    client.print(soilmoisture); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.1.7"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println( "Content-Type: application/x-www-form-urlencoded" );
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
  }
  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--&gt; connection failed\n");
  }
  client.flush();
  client.stop();
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
}
</code></pre>

<p>My PHP code is as follows:</p>

<pre><code>&lt;?php
// Prepare variables for database connection
$dbusername = "x";  // enter database username, I used "arduino" in step 2.2
$dbpassword = "y";  // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
$dbname = "arduinotest1";
// Connect to your database
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword,$dbname);
if (!$dbconnect) {
    die("Database connection failed: " . mysqli_connect_error());
}
$dbselect = mysqli_select_db($dbconnect, "arduinotest1");
if (!$dbselect) {
    die("Database selection failed: " . mysqli_connect_error());
}
// Prepare the SQL statement
$sql = "INSERT INTO arduinotest1.tabletest2 (temperature,humidity,soilmoisture) VALUES ('".$_GET["temperature"]."','".$_GET["humidity"]."','".$_GET["soilmoisture"]."')";    
// Execute SQL statement
mysqli_query($dbconnect,$sql);
?&gt;
</code></pre>
 31195: <p>As far as I can see, when you call <code>client.connect(server, 80)</code>, you call it with char[] variable that holds <code>char server[] = &quot;192.168.1.7&quot;</code>. And from library code it seems that it will call function with those parameters which will try to resolve dns but can't do it and returns <code>0</code>.</p>
<p>It calls: <code>int EthernetClient::connect(const char * host, uint16_t port)</code>
,<br>when it should call: <code>int EthernetClient::connect(IPAddress ip, uint16_t port)</code><br>
Source code: <a href="https://github.com/arduino-libraries/Ethernet/blob/master/src/EthernetClient.cpp" rel="nofollow noreferrer">https://github.com/arduino-libraries/Ethernet/blob/master/src/EthernetClient.cpp</a></p>
<p>So you need to change <code>char server[] = &quot;192.168.1.7&quot;;</code> to <code>IPAddress server(192,168,1,7);</code>
That way it will call the right connection -function and wont get stuck with dns.</p>
<p><em>Edit: added details</em></p>
