# Arduino ethernet webclient php script and mysql

**URL:** <https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098>\
**Category:** Syntax & Programs\
**Created:** [December 17, 2010, 1:42pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098 "2010-12-17T13:42:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 1:42pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/1 "2010-12-17T13:42:18Z")

</div>

Hi,

Can somebody help me out as I can't see the answer to my problem.

I'm running an Ethernet Shield with a thermistor and wish to add the values to a mysql database. I know the php script works as I can insert a number instead of (celsius)"); and then run the script and the number appears in the database. My problem is with the sketch which works accept it wont pass the data to the php file which lives at /var/www/ard\_log/update.phpThermometer0()  
Here is the sketch:

```cpp
#include <SPI.h>

#include <WString.h>
#include <Ethernet.h>

#define READING_PIN 0
/*
      Simple Ethernet Test

        Arduino server outputs simple text to browser

      The circuit:
      * Arduino Duemilanove
        * Arduino Ethernet shield
        * Basic FTDI breakout 5V
        *LED connected to GND and digital pin 4 via resistor
      
        By Minde
      http://www.sciencprog.com/
*/

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC }; //physical mac address
byte ip[] = { 192, 168, 1, 19 };                  // ip in lan
byte gateway[] = { 192, 168, 1, 1 };                  // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)             
int ledPin = 4;  // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
//#################################################
   double R1 = 10000.0; //resistance put in parallel
    double V_IN = 5.0;
    double A = 1.129148e-3;
    double B = 2.34125e-4;
    double C = 8.76741e-8;
    double K = 9.5; // mW/dec C – dissipation factor
    double SteinhartHart(double R)
    {
    // calculate temperature
    double logR  = log(R);
    double logR3 = logR * logR * logR;
    return 1.0 / (A + B * logR + C * logR3 );
    }
//#################################################
void setup(){
//start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
  pinMode(ledPin, OUTPUT);  
//enable serial datada print  
  Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
//#############################################
    double adc_raw = analogRead(READING_PIN);
    //Serial.println(adc_raw);
    double V =  adc_raw / 1024 * V_IN;
    //calculate resistance
    double R_th = (R1 * V ) / (V_IN - V);
    double kelvin = SteinhartHart(R_th) - V*V/(K * R_th);
    int celsius = kelvin - 273.15;//was double but changed to int to remove decimal places
    Serial.print(celsius);
    Serial.println();
    client.print("Temp.");
    client.print(" is ");
    client.print(celsius);
    client.println(".C");
    client.println("GET /http://192.168.1.20/ard_log/update_db.php?celsius=");
    client.println(celsius);
    client.println(" HTTP/1.1\r\n");
    client.println("Host: http://192.168.1.20");
    client.println(); 

    delay(1000);
//##############################################
         //clearing string for next read
          readString="";
          //stopping client
          client.stop();
            }
          }
        }

```

and here is the php:

```cpp
<?php
//Connect to database
$con = mysql_connect("xxxx", "xxxx", "xxxx");
if(!$con)
    {
    die('Could not connect: ' .mysql_error());
    }
mysql_select_db("1stClassElec", $con);

mysql_query("INSERT INTO device_1(temp) VALUES (celsius)");

mysql_close($con);
?>

```

Any help would be appreciated as I've spent hours on this problem

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 6:19pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/2 "2010-12-17T18:19:16Z")

</div>

Here is my latest attempt but it still doesn't work.  
database name = 1stClassElec and the table is temp\_1

```cpp
#include <SPI.h>
#include <WString.h>
#include <Ethernet.h>
#define READING_PIN 0
/*
      Simple Ethernet Test

        Arduino server outputs simple text to browser

      The circuit:
      * Arduino Duemilanove
        * Arduino Ethernet shield
        * Basic FTDI breakout 5V
        *LED connected to GND and digital pin 4 via resistor
      
        By Minde
      http://www.sciencprog.com/
*/

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC }; //physical mac address
byte ip[] = { 192, 168, 1, 19 };                  // ip in lan
byte gateway[] = { 192, 168, 1, 1 };                  // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)             
//int ledPin = 4;  // LED pin
//char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
//boolean LEDON = false; //LED status flag
//#################################################
    double R1 = 10000.0; //resistance put in parallel
    double V_IN = 5.0;
    double A = 1.129148e-3;
    double B = 2.34125e-4;
    double C = 8.76741e-8;
    double K = 9.5; // mW/dec C – dissipation factor
    double SteinhartHart(double R)
    {
    // calculate temperature
    double logR  = log(R);
    double logR3 = logR * logR * logR;
    return 1.0 / (A + B * logR + C * logR3 );
    }
//#################################################
void setup(){
//start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
//  pinMode(ledPin, OUTPUT);  
//enable serial datada print  
  Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
//#############################################
    double adc_raw = analogRead(READING_PIN);
    //Serial.println(adc_raw);
    double V =  adc_raw / 1024 * V_IN;
    //calculate resistance
    double R_th = (R1 * V ) / (V_IN - V);
    double kelvin = SteinhartHart(R_th) - V*V/(K * R_th);
    int celsius = kelvin - 273.15;//was double but changed to int to remove decimal places

    Serial.println();
    client.print("Temp.");
    client.print(" is ");
    client.print(celsius);
    client.println(".C");
    client.print("POST /http://192.168.1.20/ard_log/update_db.php?temp=");
    Serial.print("GET /ard_log/update_db.php?temp=");
    client.print(celsius);
    Serial.print(celsius);
    client.println(" HTTP/1.1");
    Serial.println(" HTTP/1.1");
    client.println("Host: http://192.168.1.20");
    Serial.println("Host: http://192.168.1.20");
    client.println("User-Agent: Arduino");
    Serial.println("User-Agent: Arduino");
    client.println("Accept: text/html");
    Serial.println("Accept: text/html"); 
    client.println(); 

    delay(1000);
//##############################################
         //clearing string for next read
          readString="";
          //stopping client
          client.stop();
            }
          }
        }

```

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [December 17, 2010, 6:53pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/3 "2010-12-17T18:53:58Z")

</div>

If the arduino server can pass the info back to a browser, then it is probably a PHP or mysql issue.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [December 17, 2010, 7:02pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/4 "2010-12-17T19:02:20Z")

</div>

Think you are messing up Client and Serial ... Keep them separated!

update: (looking again at your code made me wonder)

Do you intend to let the Arduino act as a server from which data can be GET?  
or do you intend to let the Arduino act as a Client that POST data to another webserver.

```cpp
    while (client.connected()) 
    {
    // keep it simpler ..
    int celsius = 100;

    Serial.println();
    [glow]client[/glow].print("Temp.");  // Serial??
    [glow]client[/glow].print(" is ");
    [glow]client[/glow].print(celsius);
    [glow]client[/glow].println(".C");
    client.print("[glow]POST [/glow]/http://192.168.1.20/ard_log/update_db.php?temp=");
    client.print(celsius);

    Serial.print("[glow]GET [/glow]/ard_log/update_db.php?temp=");  // [glow]GET vs POST difference[/glow]

```

'

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 7:28pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/5 "2010-12-17T19:28:02Z")

</div>

I imagine I need a client as my idea was to have the Arduino write to the mysql database, there could be more than one Arduino sending the same sort of data. When I wish to view any results I would just read the results from the database in a web page.

The Serial.print was only to check to right data was being sent but I've just tried GET and Post but still nothing appears in the database.

I still don't understand why nothing is written to the database. 😢

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [December 17, 2010, 7:43pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/6 "2010-12-17T19:43:42Z")

</div>

OK,

First you have to fix the webserver to work properly. Build two php pages, one with a form in which you can fill in two fields - time and temperature - and a second one to process the data posted and stores it into a database.

- [PHP form tutorial](http://myphpform.com/php-form-tutorial.php)

If the form works you can let the Arduino's do the posting. An example of the layout of a POST command can be found here:

- [HTTP Made Really Easy](http://www.jmarshall.com/easy/http/#postmethod)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 8:14pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/7 "2010-12-17T20:14:45Z")

</div>

robtillaart

Can you explain why I need a form as I want this to be automated and have the Arduino send data direct to the mysql database?

Surely the Arduino can do that using GET or perhaps PUT?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 10:48pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/8 "2010-12-17T22:48:30Z")

</div>

> have the Arduino send data direct to the mysql database?

That it can not do. There is nothing in MySQL that listens to the serial port. You can have the Arduino with Ethernet shield do GET, PUT, or POST, depending on which is easier for you to understand/get working. Some other application, like a PHP script, needs to get the data, and construct the SQL commands to insert the data in the database.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 17, 2010, 11:05pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/9 "2010-12-17T23:05:15Z")

</div>

robtillaart

Perhaps I've confused things more than I'm confused.

I'm trying to send data using client.print via ethernet from an Arduino client to my home web server which runs on a laptop. The serial.print is just so I can see what's happening using the "Serial Monitor".

I'm trying to get the Arduino to pass data to update\_db.php which should load it into the database 1stClassElec table device\_1.

Sorry, and grateful for your help

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [December 17, 2010, 11:27pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/10 "2010-12-17T23:27:06Z")

</div>

> I'm trying to send data using client.print via ethernet from an Arduino client to my home web server which runs on a laptop. The serial.print is just so I can see what's happening using the "Serial Monitor".
> 
> I'm trying to get the Arduino to pass data to update\_db.php which should load it into the database 1stClassElec table device\_1.

Well, the issue may not be with the arduino, but with the setup on your server. Below is some arduino ethernet test code that sends a GET request to my apache web server for the contents of the text file, and the apache web server sends the text of the file back to the arduino to verify that the ethernet shield is working. You could append your data to the URL as a query\_string to send to your server. You will have to develop the data handling programs for your server.

```arduino
//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 208, 104, 2, 86 }; // zoomkat

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 9:01am UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/11 "2010-12-18T09:01:36Z")

</div>

zoomkat, thanks for info. I've created file below, please check it through in case I'm making a mistake.

```cpp
//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 192, 168, 1, 19 };
byte server[] = { 192,168,1,20 }; // zoomkat

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /http://192.168.1.20/var/www/arduino.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

```

and the responce I get back

```cpp
starting simple arduino client test

connecting...

connected

HTTP/1.1 404 Not Found

Date: Sat, 18 Dec 2010 08:57:55 GMT

Server: Apache/2.2.14 (Ubuntu)

Vary: Accept-Encoding

Content-Length: 312

Connection: close

Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /http://192.168.1.20/var/www/arduino.txt was not found on this server.</p>
<hr>
<address>Apache/2.2.14 (Ubuntu) Server at 127.0.1.1 Port 80</address>
</body></html>

disconnecting.

==================================

```

The arduino.txt file does exist.  
I hope someone can help, thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 11:56am UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/12 "2010-12-18T11:56:08Z")

</div>

```cpp
    client.println("GET /http://192.168.1.20/var/www/arduino.txt HTTP/1.0");

```

Why is there a slash in front of http?  
If you paste the string starting with http in a browser, is the file found?

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [December 18, 2010, 12:54pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/13 "2010-12-18T12:54:11Z")

</div>

```arduino
client.println("GET /http://192.168.1.20/var/www/arduino.txt HTTP/1.0");

```

==\>

```arduino
client.println("GET /arduino.txt HTTP/1.0");

```

you allready connected to the server, port 80 == HTTP, /var/www is probably the docroot of your webserver...

- [HTTP Help: How to test HTTP using Telnet](http://www.esqsoft.com/examples/troubleshooting-http-using-telnet.htm)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 2:00pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/14 "2010-12-18T14:00:53Z")

</div>

@ PaulS. If I pasta [http://192.168.1.20/arduino.txt](http://192.168.1.20/arduino.txt) into a browser it is found and shows my inserted text.

@robtillart.

> client.println("GET /arduino.txt HTTP/1.0");

gives:

```arduino
starting simple arduino client test

connecting...

connected

HTTP/1.1 200 OK

Date: Sat, 18 Dec 2010 13:48:52 GMT

Server: Apache/2.2.14 (Ubuntu)

Last-Modified: Sat, 18 Dec 2010 08:55:24 GMT

ETag: "1fbee-5-497ab729b1eef"

Accept-Ranges: bytes

Content-Length: 5

Vary: Accept-Encoding

Connection: close

Content-Type: text/plain

test

disconnecting.

```

So is this working?

If so what is wrong with my code which stops me writing to the database?

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [December 18, 2010, 3:50pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/15 "2010-12-18T15:50:49Z")

</div>

> If so what is wrong with my code which stops me writing to the database?

The arduno is working just fine. The issue is on your server and what application you are using with apache to interpet what is sent and the manipulaton of your database. If the server is on a linux box, then you need to have some linux compatable application to perform the database operations when apache gets the incomming request. In the past I think Perl was often used as a CGI application. PHP might have a version for linux.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 4:37pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/16 "2010-12-18T16:37:11Z")

</div>

zoomkat.

Thanks for getting back. I have Xubuntu with LAMP.

If I type the value into my PHP script the database gets filled, what I don't understand is how or through what the Arduino passes info to the PHP.

If you have any ideas where I should look I'd much appreciate it.

Thanks again

Tony

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [December 18, 2010, 5:20pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/17 "2010-12-18T17:20:20Z")

</div>

You might post your PHP code that works to see what you have. Below shows how to append a GET request (query\_string, the string after the ?) to a URL. In windows, apache sets the query\_string as an environmental varable, from which an application started by apache collects the data. Note that some characters have special meaning and can't be put in a query\_string.

client.println("GET /arduino.txt HTTP/1.0");

With query\_string added:

client.println("GET /arduino.php?put-your-data-here HTTP/1.0");

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 5:42pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/18 "2010-12-18T17:42:55Z")

</div>

Well this is what I'm trying to run. The Arduino code is yours, modified to update the database with a value.

```cpp
//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 192, 168, 1, 19 };
byte server[] = { 192,168,1,20 }; // zoomkat

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    //client.println("GET /arduino.txt HTTP/1.0");
    client.println("GET /update_db.php?celsius=18 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    
    
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

```

And the PHP script

```cpp
<?php
//Connect to database 
$con = mysql_connect("localhost", "xxxx", "xxxx");
if(!$con)
      {
      die('Could not connect: ' .mysql_error());
      }
mysql_select_db("1stClassElec", $con);

mysql_query("INSERT INTO device_1(temp) VALUES (celsius)");
mysql_close($con);
?>

```

NOTE:  
If I change `mysql_query("INSERT INTO device_1(temp) VALUES (celsius)");`  
to

```cpp
mysql_query("INSERT INTO device_1(temp) VALUES (19)");

```

and then run Arduino the value of 19 gets written into the database.  
Why`mysql_query("INSERT INTO device_1(temp) VALUES (celsius)");` doesn't pass its value of celsius=18 from the Arduino script is a mystery to me at the moment.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 6:40pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/19 "2010-12-18T18:40:27Z")

</div>

In the PHP script, there are $\_GET and $\_POST variables that contain the data passed to the script, depending on the method used to to activate the script.

The $\_GET variables are name/value pairs. The value is obtained from the name:

```cpp
$val = $_GET['celcius'];

```

It is the value ($val) that you want to store in the database.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2010, 6:52pm UTC](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098/20 "2010-12-18T18:52:57Z")

</div>

@ PaulS I was just reading a web page which is similar to your explanation. My PHP script is working and now looks like this:

```cpp
<?php
//echo ("update_db.php indicator");
$celsius = $_GET['celsius'];
//Connect to database 
$con = mysql_connect("localhost", "xxxx", "xxxx");
if(!$con)
      {
      die('Could not connect: ' .mysql_error());
      }
mysql_select_db("1stClassElec", $con);

mysql_query("INSERT INTO device_1(temp) VALUES ('$celsius')");
mysql_close($con);
?>

```

Thanks go to all of you for your help, and I expect I'll be back soon with another question 🙂

[Next page](https://forum.arduino.cc/t/arduino-ethernet-webclient-php-script-and-mysql/46098.md?page=2)
