arduino read data from mysql

hey i want to use arduino to read data from my database to control hardware if data in database is 1 hardware will turn on and if data in database is 0 hardware turn off now i use arduino mega connect wifi with ethernet shield i use ethernet client not server

can i type sql code to arduino ide ?

thanks

can i type sql code to arduino ide ?

You can use the Arduino to send SQL commands over Ethernet just like any other HTTP data.

thx
u have example of it ?

As a start, if you use the Ethernet client on the Arduino to send requests to the server you will get a response which you can display in the Arduino Serial monitor window.

Have you got that far ?

now i can send data of sensor to my database

but now i need to read data in my database to control my hardware

thank you

Post the code of what you currently have working. Please read the advice on posting code in the forum stickies.

this is my arduino code

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */

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

#include "DHT.h"
#define DHTPIN 22
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "xxx.com";    // name address for Google (using DNS)


IPAddress ip(192,168,1,109);


int temp;
int moi;

EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server,80)) {
    Serial.println("connected");
    // Make a HTTP request:
    temp = dht.readTemperature();//dht11
    moi = analogRead(15); //soil moisture
    Serial.println(temp);
    Serial.println(moi);
    Serial.print("server name : ");
    Serial.println(server);
    client.print( "GET /add.php?"); // php??
   client.print("temp1=");
   //client.print(tempValue);
   client.print(temp);
   client.print("&&");
   client.print("moi1=");
   //client.print(levelValue);
   client.print(moi);
   client.println( " HTTP/1.1");
   client.println( "Host: xxx.com" );//ur web server
   client.println( "Content-Type: application/x-www-form-urlencoded" );
   client.println( "Connection: close" );
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

add.php

<?php
   include("conec.php");
   $link=Conection();
$Sql="insert into tempmoi (temp1,moi1)  values ('".$_GET["temp1"]."', '".$_GET["moi1"]."')";
   mysql_query($Sql,$link);
   header("Location: insertareg.php");
?>

conec.php

<?php
function Conection(){
   if (!($link=mysql_connect("localhost","user","pass")))  {
      exit();
   }
   if (!mysql_select_db("table",$link)){
      exit();
   }
   return $link;
}
?>

insertareg.php

<html>
<head>
   <title>Data of Sensor</title>
</head>
<body>
<h1>Data from the temperature and moisture sensors</h1>
<form action="add.php" method="get">
<TABLE>
<tr>
   <td>Temperature 1</td>
   <td><input type="text" name="temp1" size="20" maxlength="30"></td>
</tr>
<tr>
   <td>Moisture 1</td>
   <td><input type="text" name="moi1" size="20" maxlength="30"></td>
</tr>
</TABLE>
<input type="submit" name="accion" value="enter">
</FORM>
<hr>

<?php
   include("conec.php");
   $link=Conection();
   $result=mysql_query("select * from tempmoi order by id desc",$link);
?>
<table border="1" cellspacing="1" cellpadding="1">
      <tr>
         <td>&nbsp;Date and Time&nbsp;</td>
         <td>&nbsp;Temperature 1&nbsp;</td>
         <td>&nbsp;Moisture 1&nbsp;</td>
       </tr>
<?php     
   while($row = mysql_fetch_array($result)) {
printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td></tr>", $row["dat"],$row["temp1"], $row["moi1"]);
   }
   mysql_free_result($result);
?>
</table>
</body>
</html>

this is my reference http://www.instructables.com/id/Save-data-of-temperature-and-humidity-on-MySQL-wit/?ALLSTEPS

Do you get anything back from the server ?

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

Do you get anything back from the server ?

UKHeliBob can you please tell me what else do you we need to include to get a data from server to arduino? Because i too got struck in same place now.

UKHeliBob can you please tell me what else do you we need to include to get a data from server to arduino?

If you are not connecting to the server, it can't possibly be made to send a reply.

If you are trying to access a script that doesn't exist, or isn't where you think it is, the script can't possibly return anything.

We can't tell what is wrong with your code or your network.