reading from sql with php

hi

am having a little problem.
i can send data to sql with php thank tot tutorials.
but i never find a way to read data from sql and put it in the arduino.

i want to use it to control the arduino over the web.

grts

You cannot run PHP code on an Arduino. You need a PC or a RaspberryPi to do that.

…R

Robin2:
You cannot run PHP code on an Arduino. You need a PC or a RaspberryPi to do that.

...R

Thanks for the respons

I am using a Synology with web and SQL server .
I can send to the SQL with php but Cant receive anything.

It would be really helpful if i had An example .

Grts

Are you using an arduino to create a php script and send the data, or do you mean you have tried a simple POST or GET with an esp8266 library or whatever, I have done both of those fine and a html form with post but not an actual php script on arduino via a esp. However i have created a php embedded in html script on a server which selects the data from a sql database every time the url is accessed, such as from my esp8266 and then posts the data to with a form and using an empty action i can access the same url and then reading the header with client.read on my arduino via rx,tx i look for the = or & i can pass the data into a buffer char array so it is possible to retrieve data from sql database but not directly and you have to watch out for sql injection or cross site scripting.

Im sure you know how to write php but here's a link anyway
https://www.w3schools.com/php/php_mysql_select.asp
and there are many esp library examples to read data from a url, it then just a matter of putting it into an array

I can send to the SQL with php but Cant receive anything.

So, you have some Arduino code, that you didn’t show us, that makes a GET request, and may, or may not, actually read the server response.

The GET request is to a PHP script that may, or may not, actually return anything.

You “get nothing”, but have not provided us with anywhere near enough information to determine if that is, or is not, correct behavior.

hi thanks for all the replay’s!!!

here is my php code for sending to the sql

<?php
   	include("connect.php");
   	
   	$link=Connection();

	$temp1=$_POST["temp1"];
	$hum1=$_POST["hum1"];
    $light1=$_POST["light1"];
	
	
	$query = "INSERT INTO `data` (`temperature`, `humidity`, `light`) 
		VALUES ('".$temp1."','".$hum1."','".$light1."')"; 
   	
   	mysql_query($query,$link);
	mysql_close($link);

   	header("Location: index.php");
?>

and here is my arduino code

// Include the required Wire library for I2C
#include <Wire.h>
#include <Ethernet.h>
#include <SPI.h>
#include <DHT.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;



#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL

int t = 0;  // TEMPERATURE VAR
int h = 0;  // HUMIDITY VAR
int l = 0; // light sensor
int g = 0;
int ground = 0;


String data;
String data_read;

int x;

void setup() {
  // Start the I2C Bus as Master
  Wire.begin();
  Serial.begin(9600);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }




  dht.begin();
  delay(10000); // GIVE THE SENSOR SOME TIME TO START



  data = "";
  data_read = "";
}

void loop() {
  x = Serial.parseInt();



  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x
  Wire.endTransmission();    // stop transmitting
  delay(1000);





  Wire.requestFrom(9, 4);

  while (Wire.available())   // slave may send less than requested
  {
    t = Wire.read();    // receive a byte as character
    h = Wire.read();
    l = Wire.read();
    g = Wire.read();





    Serial.print(t);//temperatuur
    Serial.println(" Graden Celsius");

    Serial.print(h);//vochtigheid
    Serial.println(" % Vochtigheid");

    Serial.print(l);//licht
    Serial.println(" Licht");

    Serial.print(g);//grond vochtigheid
    Serial.println(" Grond Vochtigheid");
  }

  delay(500);


//Sending to SQL

  data = "&temp1=" + String(t) + "&hum1=" + String(h) + "&light1=" + String(l) + "&ground1=" + String(g);


  if (client.connect("192.168.1.219", 80)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("Server connection OK");
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: 192.168.1.219"); // SERVER ADDRESS HERE TOO
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
     Serial.println(data);
    client.print(data);
  }

  if (client.connected()) {
    client.stop();  // DISCONNECT FROM THE SERVER
  }

  delay(1000);


//receive from SQL here is my problem 
data_read = "&ground=" + String(ground);


  if (client.connect("192.168.1.219", 80)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("Server connection OK");
    client.println("POST /read.php HTTP/1.1");
    client.println("Host: 192.168.1.219"); // SERVER ADDRESS HERE TOO
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data_read.length());
    client.println();
     Serial.println(data_read);
    client.print(data_read);


  }

  if (client.connected()) {
    client.stop();  // DISCONNECT FROM THE SERVER
  }

  delay(1000);
Serial.print(ground);//grond vochtigheid
    Serial.println(" Grond Ingesteld");



  x = Serial.parseInt();


  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x
  Wire.endTransmission();    // stop transmitting
  delay(1000);


}

i cant find how to make a php,its a while i use php:)

thanks for the help

Look at your php code. It generates NO output. There are no echo statements, no print statements.

What do you expect to get back?

hi , i know it is the php for sending.

here is a php that i found online but i dont know how to put an INT from sql trough php into the arduino

<?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>';
    }
?>

grts
thanks for the help

steenmans:
here is a php that i found online but i dont know how to put an INT from sql trough php into the arduino

IMHO you will find the learning much easier if you put the Arduino to one side for a while and concentrate on writing PHP code that takes info from a browser on your PC and saves it in the database, or vice versa.

There will be hundreds of online tutorials about doing that. Just keep it very simple as you know it will eventually need to work with an Arduino.

When it works in your browser you will just need to write Arduino code to take the place of the browser.

...R

i dont know how to put an INT from sql trough php into the arduino

Can you guess, or do some research to determine, what the echo command does?