-->
Page 1 of 1

ESP8266 - How to get data from BD using HTTP + PHP + MySQL

PostPosted: Tue Oct 02, 2018 8:48 am
by marceloseibt
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.

Re: ESP8266 - How to get data from BD using HTTP + PHP + MyS

PostPosted: Tue Oct 09, 2018 8:00 am
by QuickFix
Are you sure your question doesn't actually belong in the ** Newbie Corner **-forum?

You might also consider writing your question in your native language (Spanish?) and use Google Translate to translate it to English, since I'm not really sure I understand what you're looking for. :?

Also, please put any code between [ CODE ]-tags as it's easier for others to read. :idea:

[EDIT]
Having had a read a couple of times over: do you mean you want to parse the result of a request to a webserver (running PHP and extracting data from a MySQL database) using an ESP8266?

Luckily you don't actually have to use HTML, since you're only seeking raw data from the DB.
Just create a PHP-script that will query the database and echo's a raw result.
How to do this is beyond the scope of this forum (this is an ESP forum, not a PHP-forum), but there are loads of websites and forums that can help you with that.

Once you've got that running: use HTTP client example code that will request a webpage (without a parameter to begin with) and outputs the result to the UART-port, so you can simply monitor it.
Try to figure out how that works and when you do, just parse the result with self written code.

It really shouldn't be too difficult to do.
Off course: if you're stuck or are having a specific ESP question, you're always welcome to come by.
Most of us also speak (a bit of) PHP, so we might get you on your way if it's not too specific.

Re: ESP8266 - How to get data from BD using HTTP + PHP + MyS

PostPosted: Tue Oct 09, 2018 1:38 pm
by marceloseibt
QuickFix wrote:Are you sure your question doesn't actually belong in the ** Newbie Corner **-forum?

You might also consider writing your question in your native language (Spanish?) and use Google Translate to translate it to English, since I'm not really sure I understand what you're looking for. :?

Also, please put any code between [ CODE ]-tags as it's easier for others to read. :idea:

[EDIT]
Having had a read a couple of times over: do you mean you want to parse the result of a request to a webserver (running PHP and extracting data from a MySQL database) using an ESP8266?

Luckily you don't actually have to use HTML, since you're only seeking raw data from the DB.
Just create a PHP-script that will query the database and echo's a raw result.
How to do this is beyond the scope of this forum (this is an ESP forum, not a PHP-forum), but there are loads of websites and forums that can help you with that.

Once you've got that running: use HTTP client example code that will request a webpage (without a parameter to begin with) and outputs the result to the UART-port, so you can simply monitor it.
Try to figure out how that works and when you do, just parse the result with self written code.

It really shouldn't be too difficult to do.
Off course: if you're stuck or are having a specific ESP question, you're always welcome to come by.
Most of us also speak (a bit of) PHP, so we might get you on your way if it's not too specific.


Thank you! I already did it! And i agree with you about your
QuickFix wrote:Are you sure your question doesn't actually belong in the ** Newbie Corner **-forum?

You might also consider writing your question in your native language (Spanish?) and use Google Translate to translate it to English, since I'm not really sure I understand what you're looking for. :?

Also, please put any code between [ CODE ]-tags as it's easier for others to read. :idea:

[EDIT]
Having had a read a couple of times over: do you mean you want to parse the result of a request to a webserver (running PHP and extracting data from a MySQL database) using an ESP8266?

Luckily you don't actually have to use HTML, since you're only seeking raw data from the DB.
Just create a PHP-script that will query the database and echo's a raw result.
How to do this is beyond the scope of this forum (this is an ESP forum, not a PHP-forum), but there are loads of websites and forums that can help you with that.

Once you've got that running: use HTTP client example code that will request a webpage (without a parameter to begin with) and outputs the result to the UART-port, so you can simply monitor it.
Try to figure out how that works and when you do, just parse the result with self written code.

It really shouldn't be too difficult to do.
Off course: if you're stuck or are having a specific ESP question, you're always welcome to come by.
Most of us also speak (a bit of) PHP, so we might get you on your way if it's not too specific.


Thank you very much for the advices ! I already managed to do this, exactly the way you explained it.
Im going to remove this topic! Its more a php issue, for sure!