Lua connect with network

Good afternoon, I have a php file hosted on a site that has the following code:

$server = "server\_name"; $user = "user\_name"; $pass = "my\_pass"; $dbname = "teste"; // conec $conn = mysqli\_connect($server, $user, $pass, $dbname); $result\_pesquisa = "SELECT \* FROM teste"; $resultado\_pesquisa = mysqli\_query($conn, $result\_pesquisa); $usuario = "select \* from teste order by id desc limit 1"; $cor = mysqli\_query($usuario);

I would like to know how do I get the value of the $ color variable using LUA …

I’m very lost in this part, do any of you have any idea to save me? Thank you !! <3

I don’t see a $color variable in your PHP script. 

Also normally after you do a database query, you’re given a “result” set, not individual items. Your query can typically return multiple rows with multiple data fields. Normally there is a while loop and a command to fetch a row from the result set that returns an associative array. Then you can take the table of data to access individual fields.

It’s going to be hard to be more specific without knowing the scheme of your database tables.

Rob

Rob, it’s a simple table I just created, I just want to get the result of the “user”.

I put: $ user = “select * from test order by id desc limit 1”;

because I was reading that this would bring me the result of the value of the entire line that occupies the last id.

I really suck at php / mysql but in my eyes it seems like this was supposed to work, right?  :frowning:

(my table is attached)

Or it was to be attached hahahahhah anyway, this is the link, if they can not see:
https://ibb.co/gHnBfd

Here is the basics…

$mysqli&nbsp;=&nbsp;new&nbsp;mysqli($server,&nbsp;$user&nbsp;,&nbsp;$pass,&nbsp;$dbname); $sql&nbsp;=&nbsp;"select \* from teste where id=1"; $result&nbsp;=&nbsp;$mysqli-\>query($sql)) $data =&nbsp;$result-\>fetch\_assoc(); echo&nbsp;"Name = "&nbsp;.&nbsp;$data ['nome']; $result-\>free(); $mysqli-\>close();

If you need more help then I suggest using the MySQL forums - https://forums.mysql.com/

Thank you very much SGS, but we ended up leaving the main subject (anyway, thank you for the help and link hahahah)

About Corona, how would I connect to this webpage to get the variable $data[‘name’]? My main question is this :frowning:

You access the script with the network.request() API call.  You give it the URL of your script, pass any HTTP GET or POST data items. Then when the script completes, your network.request()'s call back function will be called with the output of the script. It’s recommended that you use output your data in JSON format then when you get it back in the script, use the built in json.decode() function to turn the script output into a Lua table.

https://docs.coronalabs.com/api/library/network/request.html

Rob

Thank you Rob for helping me, I got what I wanted.

If someone comes with the same question, this is the result:

PHP File:

\<?php $server = "YOUR\_SERVER"; $user = "YOUR\_USER"; $pass = "YOUR\_PASS"; $namedb = "YOUR\_DB"; // conec $conect = mysqli\_conectect($server, $user, $pass, $namedb); if ($conect-\>conectect\_error) { die("conectection failed: " . $conect-\>conectect\_error); } $sql = "SELECT id, name, user FROM tst"; $result = $conect-\>query($sql); if ($result-\>num\_rows \> 0) { // output data of each row while($row = $result-\>fetch\_assoc()) { $id = $row["id"]; $usuario = $row["user"]; $nome = $row["name"]; echo $id; echo $usuario; echo $nome; } } else { echo "0 results"; } $conect-\>close(); ?\>

Lua code is a sample network request:

local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else print ( event.response ) end end network.request( "YOUR\_SERVER", "GET", networkListener )

:smiley:

I don’t see a $color variable in your PHP script. 

Also normally after you do a database query, you’re given a “result” set, not individual items. Your query can typically return multiple rows with multiple data fields. Normally there is a while loop and a command to fetch a row from the result set that returns an associative array. Then you can take the table of data to access individual fields.

It’s going to be hard to be more specific without knowing the scheme of your database tables.

Rob

Rob, it’s a simple table I just created, I just want to get the result of the “user”.

I put: $ user = “select * from test order by id desc limit 1”;

because I was reading that this would bring me the result of the value of the entire line that occupies the last id.

I really suck at php / mysql but in my eyes it seems like this was supposed to work, right?  :frowning:

(my table is attached)

Or it was to be attached hahahahhah anyway, this is the link, if they can not see:
https://ibb.co/gHnBfd

Here is the basics…

$mysqli&nbsp;=&nbsp;new&nbsp;mysqli($server,&nbsp;$user&nbsp;,&nbsp;$pass,&nbsp;$dbname); $sql&nbsp;=&nbsp;"select \* from teste where id=1"; $result&nbsp;=&nbsp;$mysqli-\>query($sql)) $data =&nbsp;$result-\>fetch\_assoc(); echo&nbsp;"Name = "&nbsp;.&nbsp;$data ['nome']; $result-\>free(); $mysqli-\>close();

If you need more help then I suggest using the MySQL forums - https://forums.mysql.com/

Thank you very much SGS, but we ended up leaving the main subject (anyway, thank you for the help and link hahahah)

About Corona, how would I connect to this webpage to get the variable $data[‘name’]? My main question is this :frowning:

You access the script with the network.request() API call.  You give it the URL of your script, pass any HTTP GET or POST data items. Then when the script completes, your network.request()'s call back function will be called with the output of the script. It’s recommended that you use output your data in JSON format then when you get it back in the script, use the built in json.decode() function to turn the script output into a Lua table.

https://docs.coronalabs.com/api/library/network/request.html

Rob

Thank you Rob for helping me, I got what I wanted.

If someone comes with the same question, this is the result:

PHP File:

\<?php $server = "YOUR\_SERVER"; $user = "YOUR\_USER"; $pass = "YOUR\_PASS"; $namedb = "YOUR\_DB"; // conec $conect = mysqli\_conectect($server, $user, $pass, $namedb); if ($conect-\>conectect\_error) { die("conectection failed: " . $conect-\>conectect\_error); } $sql = "SELECT id, name, user FROM tst"; $result = $conect-\>query($sql); if ($result-\>num\_rows \> 0) { // output data of each row while($row = $result-\>fetch\_assoc()) { $id = $row["id"]; $usuario = $row["user"]; $nome = $row["name"]; echo $id; echo $usuario; echo $nome; } } else { echo "0 results"; } $conect-\>close(); ?\>

Lua code is a sample network request:

local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else print ( event.response ) end end network.request( "YOUR\_SERVER", "GET", networkListener )

:smiley: