0

What is the best way to send a variable to a remote php file and receive the rest of the data in the MySQL row in seperate variables (Only one variable will be sent). I already have the variable to send saved as 'name' This is my PHP

<?php

// Make a MySQL Connection
mysql_connect("mysql8.000webhost.com", "a6811170_NTUDB", "?????") or die(mysql_error());

// Select the database
mysql_select_db("a6811170_NTUDB") or die(mysql_error());

//Get Name
$name=name; //Whatever is sent

// Get data from the table
$result = mysql_query("SELECT * FROM SocietyDatabase WHERE Name=$name") or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {
print(json_encode($row['Name']));
print(json_encode($row['President']));
print(json_encode($row['Description']));
} 
?>
Ryaller
  • 77
  • 3
  • 12

2 Answers2

1

Call your php page from your app with your variable in the url (GET) or in POST and parse the result as JSON.

Edit : to call your php script from your app : Make an HTTP request with android

And prefer to use PDO, mysql_ functions are marked as deprecated since php 5.5.0. With PDO it would be :

<?php

$db = new PDO('mysql:host=mysql8.000webhost.com;dbname=a6811170_NTUDB', 'a6811170_NTUDB', 'PASSWORD');
$query = $db->prepare("SELECT * FROM SocietyDatabase WHERE Name=?");
$query->execute(array($_GET['name']));
$result = $query->fetchAll();

print(json_encode($result));

?>

That way the input parameter is protected from injections, and you are using up-to-date functions.

Community
  • 1
  • 1
dqms
  • 303
  • 1
  • 10
0

Best way for short data ist http "get" you call

http://www.myserver.de/myscript.php?name=honkytonk

You find the contents of "name" in $_REQUEST['name'].

If your data gets bigger use a post request. For Pictures or Files use a Post with Content/Multipart Data.

On the side:

Always escape your external data or you will most certainly be hacked!

like this:

// Get data from the table

$result = mysql_query("SELECT * FROM SocietyDatabase WHERE 
       Name='".mysql_real_escape_string($name)."'") or die(mysql_error());
snitch182
  • 723
  • 11
  • 21
  • so how do i send the data back in the variable at the end of my PHP? – Ryaller Apr 10 '13 at 15:26
  • I changed my answer to reflect that. – snitch182 Apr 10 '13 at 15:30
  • You mean to get the result of your php script in your android app ? The HTTP request result will contain the JSON printed by your php, so all you have to do is parsing this JSON to extract the data. – dqms Apr 10 '13 at 15:33
  • Also, it is recommended to use PDO to query your base as mysql_* functions are deprecated. – dqms Apr 10 '13 at 15:35