water temperature data into mysql using ethernet shield

from reference http://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-push/#comment-31652
in this project i use: PC -OS Windows, Xampp, arduino UNO R3

i also make modification on 3 files php: dbconnect.php, add_data.php, review_data.php

------------------------------dbconnect.php-----------------------------------------------

<?php 
$dbhost = 'localhost';
$dbuser = 'root'; //never use root user in live system
$dbpass = ''; // never use blank or simple password or password same as user name in live system
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'test';
mysql_select_db($dbname);
?>

-------------------------add_data.php------------------------------------------------------

<?php
    // Connect to MySQL
    include("dbconnect.php");

    $counter=1;

    while ( isset( $_GET["serial".$counter] ) )
    {
        // Prepare the SQL statement
        $SQL = "INSERT INTO test.temperature (sensor ,celsius) VALUES ('".$_GET["serial".$counter]."', '".$_GET["temperature".$counter]."')";     

        // Execute SQL statement
        mysql_query($SQL);

        // Increase counter
        $counter++;
    }

    // Go to the review_data.php 
    header("Location: review_data.php");
?>

--------------------------------review_data.php---------------------------------------------

<?php 
    // Start MySQL Connection
    include('dbconnect.php'); 
?>

<html>
<head>
    <title>Arduino Temperature Log</title>
    <style type="text/css">
        .table_titles, .table_cells_odd, .table_cells_even {
                padding-right: 20px;
                padding-left: 20px;
                color: #000;
        }
        .table_titles {
            color: #FFF;
            background-color: #666;
        }
        .table_cells_odd {
            background-color: #CCC;
        }
        .table_cells_even {
            background-color: #FAFAFA;
        }
        table {
            border: 2px solid #333;
        }
        body { font-family: "Trebuchet MS", Arial; }
    </style>
</head>

    <body>
        <h1>Arduino Temperature Log</h1>
    <table border="0" cellspacing="0" cellpadding="4">
      <tr>
            <td class="table_titles">ID</td>
            <td class="table_titles">Date and Time</td>
            <td class="table_titles">Sensor Serial</td>
            <td class="table_titles">Temperature in Celsius</td>
          </tr>
<?php
    // Retrieve all records and display them
    $result = mysql_query("SELECT * FROM temperature ORDER BY id ASC");

    // Used for row color toggle
    $oddrow = true;

    // process every record
    while( $row = mysql_fetch_array($result) )
    {
        if ($oddrow) 
        { 
            $css_class=' class="table_cells_odd"'; 
        }
        else
        { 
            $css_class=' class="table_cells_even"'; 
        }

        $oddrow = !$oddrow;

        echo '<tr>';
        echo '   <td'.$css_class.'>'.$row["id"].'</td>';
        echo '   <td'.$css_class.'>'.$row["event"].'</td>';
        echo '   <td'.$css_class.'>'.$row["sensor"].'</td>';
        echo '   <td'.$css_class.'>'.$row["celsius"].'</td>';
        echo '</tr>';
    }
?>
    </table>
    </body>
</html>

----------------------sketch on arduino-----------------------------------------------------------

#include <UIPEthernet.h> // Used for Ethernet
#include <OneWire.h>     // Used for temperature sensor(s)

// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };                                      
// For the rest we use DHCP (IP address and such)


IPAddress server(192,168,1,2); // IP Adres (or name) of server to dump data to
int  interval = 1000; // Wait between dumps
IPAddress ip(192,168,1,3);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;

// **** TEMPERATURE SETTINGS ****
// Sensor(s) data pin is connected to Arduino pin 2 in non-parasite mode!
OneWire  ds(3);

void setup() {

  Serial.begin(19200);
   Ethernet.begin(mac, ip);
   delay(1000);

  Serial.println("Temperature Connection to Server");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask       : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP     : ");
  Serial.println(Ethernet.dnsServerIP());
}

void loop() {
  // if you get a connection, report back via serial:
 

  if (client.connect(server, 80)) {
    delay(1000);
    client.connect(server, 80);
    Serial.println("-> Connected");

    // Make a HTTP request:
    //client.print( "GET /testserver/arduino_temperatures/add_data.php?");
    client.print( "GET /add_data.php?");

    TemperaturesToGetVariables(); // send serial and temperature readings

    client.println( " HTTP/1.1");
    client.println( "Host: 192.168.1.2" );
    client.print(" Host: ");
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  }
  else
  {
    // you didn't get a connection to the server:
    Serial.println("--> connection failed!");
    client.stop();
    delay(1000);
    Ethernet.begin(mac, ip, gateway, subnet);
  }

  delay(interval);
}

void TemperaturesToGetVariables(void) {
  byte counter;
  byte present = 0;
  byte sensor_type;
  byte data[12];
  byte addr[8];
  float celsius;
  byte sensorcounter;

  ds.reset_search();
  sensorcounter = 1; // we start counting with sensor number 1

  while ( ds.search(addr) )
  {
    if (sensorcounter>1) client.print("&&"); // add ampersand if not first sensor

    client.print("serial"); // print: sensorx=
    client.print(sensorcounter);
    client.print("=");

    // Print Serial number
    Serial.print("   Sensor     : ");
    Serial.println(sensorcounter);
    Serial.print("   Serial     : ");

    for( counter = 0; counter < 8; counter++)
    {
      if (addr[counter]<10) client.print("0");
      client.print(String(addr[counter], HEX));
      if (counter<7) client.print("%20");

      if (addr[counter]<10) Serial.print("0");
      Serial.print(String(addr[counter], HEX));
      if (counter<7) Serial.print(" ");
    }
    Serial.println();

    client.print("&&temperature");  // print: &temperaturex=
    client.print(sensorcounter);
    client.print("=");

    // Check CRC
    if (OneWire::crc8(addr, 7) != addr[7]) // print ERROR if CRC error
    {
        client.println("ERROR");
    }
    else // CRC is OK
    {    
        // Removed sensor type detection and assumed DS18B20 sensor
        ds.reset();
        ds.select(addr);
        ds.write(0x44);  // start conversion, with regular (non-parasite!) power

        delay(750);     // maybe 750ms is enough, maybe not

        present = ds.reset();
        ds.select(addr);    
        ds.write(0xBE);  // Read Scratchpad

        // Get Raw Temp Data
        for ( counter = 0; counter < 9; counter++)
        {           // we need 9 bytes
          data[counter] = ds.read();
        }

        // Convert the data to actual temperature
        int16_t raw = (data[1] << 8) | data[0];

        // at lower res, the low bits are undefined, so let's zero them
        byte cfg = (data[4] & 0x60);

        //// default is 12 bit resolution, 750 ms conversion time
        if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
        else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
        else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms

        celsius = (float)raw / 16.0;
        client.print(celsius);

        Serial.print("   Temperature: ");
        Serial.print(celsius);
        Serial.println(" C");
     }

     sensorcounter++;
  }
  return;
}

the problem is i cant connect to mysql and connection always fail…

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

log.png

connection fail.png

htdocs.rar (1.46 KB)

Are you sure your IP addresses are correct?

Try ethernet.begin without an IP in the hope that your router will give you one - at least it'll demonstrate that there's enough communication for DHCP to work.

  if (client.connect(server, 80)) {
    delay(1000);
    client.connect(server, 80);
    Serial.println("-> Connected");

If you successfully connect to the server, connect to the server. Why?