ESP8266 - How to get data from BD using HTTP + PHP + MySQL
Posted: Tue Oct 02, 2018 8:48 am
Hi, mates! This is my first time here, hope I can also help some of you in a future opportunity. Im trying to get data from the MySQL database, which is running in my own computer, in the localhost.
This is the ESP8266 code that I am using for this:
##### Function for request ###########
void consultaBanco(string nomeVariavel, int valor)
{
WiFiClient client = server.available();
HTTPClient http;
switch(nomeVariavel){
case id_usuario:
String url = get_host+"C:\xampp\htdocs\reservasala\consultaBanco.php?nomeVariavel="+idUsuario+"&valor="+valor;
break;
default:
break;
}
http.begin(url);
//GET method
int httpCode = http.GET();
String retorno = http.getString();
Serial.println(retorno);
http.end();
delay(1000);
}
##################################### PHP code #########
<?php
$nomeVariavel = filter_input(INPUT_GET, 'nomeVariavel');
$num_idReserva = filter_input(INPUT_GET, 'valor');
$conn = mysqli_connect('localhost', 'root', '', 'reservasala');
Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
switch ($nomeVariavel) {
case 'id_reserva':
$sql = "SELECT reserva.autorizado FROM reserva WHERE reserva.id_reserva = '$num_idReserva' LIMIT 1";
#############################
This is an example of how Im trying to associate the requests. This is the question I have about it: How do I return data for the ESP8266 client from the PHP code.
Im rookie about HTTP requests and web codes.
I would appreciate any answer or link with something in this direction.
Ty very much.
Marcelo.
This is the ESP8266 code that I am using for this:
##### Function for request ###########
void consultaBanco(string nomeVariavel, int valor)
{
WiFiClient client = server.available();
HTTPClient http;
switch(nomeVariavel){
case id_usuario:
String url = get_host+"C:\xampp\htdocs\reservasala\consultaBanco.php?nomeVariavel="+idUsuario+"&valor="+valor;
break;
default:
break;
}
http.begin(url);
//GET method
int httpCode = http.GET();
String retorno = http.getString();
Serial.println(retorno);
http.end();
delay(1000);
}
##################################### PHP code #########
<?php
$nomeVariavel = filter_input(INPUT_GET, 'nomeVariavel');
$num_idReserva = filter_input(INPUT_GET, 'valor');
$conn = mysqli_connect('localhost', 'root', '', 'reservasala');
Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
switch ($nomeVariavel) {
case 'id_reserva':
$sql = "SELECT reserva.autorizado FROM reserva WHERE reserva.id_reserva = '$num_idReserva' LIMIT 1";
#############################
This is an example of how Im trying to associate the requests. This is the question I have about it: How do I return data for the ESP8266 client from the PHP code.
Im rookie about HTTP requests and web codes.
I would appreciate any answer or link with something in this direction.
Ty very much.
Marcelo.