HI Guys,
I have following problem. I’m trying to connect from my Iphone app to a php site, to access the mySql db, to get the right information.
This is my code:
<?php
mysql_connect ("localhost", "user", "pass") or die (mysql_error());
echo "Connected to MySql <br /><hr />";
mysql_select_db ("database_com") or die (mysql_error());
echo "Connectted to Database <br /><hr />";
$country = $_POST['country'];// THIS IS THE VALUE I WANT TO LOAD INTO THE SELECT STATEMENT
echo "value <br /><hr />" . $country;
$query = "SELECT * FROM world WHERE land='$country'";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$1 = $row['1'];
$2 = $row['2'];
$3 = $row['a3'];
$4 = $row['4'];
$xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<country></country>";
$xmlobj = simplexml_load_string($xmltext);
$xmlobj->addChild("1", $1);
$xmlobj->addChild("2", $2);
$xmlobj->addChild("a3", $a3);
$xmlobj->addChild("4", $4);
print header("Content-type: text/plain") . $xmlobj->asXML(); // Place future code ABOVE this line
$xml->save(statistic.xml);
}
?>
If I hardcode the value of land = “Germany” i get an answer, but if I do the other thing, nothing comes out of it.
I hope u can help me.
Do you have a form that posts a country to your php page? If so, is the form using method=”post”? If you are sticking the country in the url, use $_GET instead of $_POST.
Also, call mysql_real_escape_string($_POST[‘country’]) to avoid SQL injection issues (like someone deleting all your databases).
Also, this line:
has a number of issues. It should be:
However, you cannot send headers after printing other stuff out – like your echo calls early in the script. It doesn’t sound like this is a problem yet, but it will be when your query works.
Also, you’re looping through the result set. If you expect only one result, ditch the while loop. If you have more than one result, you will get errors because of the header() call.