views:

148

answers:

8

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.

+1  A: 

What happens if you print_r or var_dump($_POST)? At first glance, it looks like your POST is empty, or at least $_POST['country'].

mabwi
Yeah the post was empty. :-) duuno how it happend, but now it is back on track, and it is working :-)
Poul John
+1  A: 

Have you check the $_POST['country'] actually contains a value?

Try

echo $query;

and update the post so we can see the content.

Pino
+4  A: 

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:

print header("Content-type: text/plain") . $xmlobj->asXML();

has a number of issues. It should be:

header("Content-type: text/plain");
print $xmlobj->asXML();

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.

Scott Saunders
Thanks you for this. I was here trying to print XML out, and now it is working.
Poul John
A: 

is the row in the database? i think there is no matching row found.

Salandur
A: 

Try a simple error catch:

<?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;
if (!isset($country) || $country == FALSE /* other fail conditions */) {
  echo "<p>There was a problem; one, or more, error condition occurred:</p>";
  echo "<pre>" . print_r(get_defined_vars(),true) . "</pre>";
}
else {
    // database access stuff
}
?>

This way, if the values aren't set (or error-tests are met) you get a display of all the variables currently defined and the database access doesn't occur.

It's crude, but it sometimes helps to see what's happening. Though, personally, I think it far more likely that you're simply experiencing a typo $_POST['country'] instead of $_POST['contry'] or something, or using $_POST instead of $_GET.

David Thomas
A: 

Could it be that this variable is undefined?

    $xmlobj->addChild("a3", $a3);

You're defining $3 above it, but I don't see $a3 defined anywhere.

On another note: Have you considered changing $1, $2, $3, $4, etc? Those are a bit Perl-ish variable names.

Christopher W. Allen-Poole
A: 

A good way to debug this is to put this block at the top of your file:

<?php
if (!isset($_POST)) {
    die('A $_POST is required for this page.');
}
...
Martin Bean
A: 

Got it to work guys. I was refering bad in my form :-( (STUPID ME), and also needed to put that incjetion thing on, so i can prevent it from expose.

Thx for help guys. Really fast I must say :-)

Poul John