Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Lua connect with network
Started by pabloapdz May 17 2018 10:56 AM

6 replies to this topic

Best Answer Rob Miracle , 18 May 2018 - 06:42 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 100 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,308 posts
  • Corona Staff

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


  • pabloapdz likes this

[TOPIC: post.html]
#3

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 100 posts
  • Corona SDK

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?  :(
 
(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


[TOPIC: post.html]
#4

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,840 posts
  • Corona SDK

Here is the basics...

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

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


  • pabloapdz likes this

[TOPIC: post.html]
#5

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 100 posts
  • Corona SDK

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 :(


[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,308 posts
  • Corona Staff

  Best Answer

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


  • pabloapdz likes this

[TOPIC: post.html]
#7

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 100 posts
  • Corona SDK

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 )

:D




[topic_controls]
[/topic_controls]