1

My Logcat shows me (correctly) all messages that I want returned, but when I ask the code to display the same thing on another php page, it goes completely wack.

Note that is this code, $from = "r", and $to = "".

Here is my code:

<?php

session_start();

/* connect to database */
$db = mysql_connect("localhost", "root", "root");

if (!$db)
    die('could not connect');

mysql_select_db('androidp2p')
    or die("could not select database");

/* variables */
$from = mysql_real_escape_string($_POST['from']); 
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

/* conditional code */
if (isset ($POST['to']))
{
    /* user wants to send a message */
    $query = "INSERT INTO messages (fromuser, touser, message) VALUES ('$from', '$to', '$message')";

    mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    echo "ok. Messages have been saved.";
}
else //if (!isset ($POST['to']))
{
    /* user wants to retrieve his messages */
    $query = "SELECT * FROM messages WHERE touser='$from'";

    /* echo test 1 */
    echo $query;

    $result = mysql_query($query)
        or die("\n\ndatabase error!\n". mysql_error());

    $mailbox = array();

    while($row = mysql_fetch_assoc($result))
    {
    $mailbox[] = $row;
    }

    /* echo test 2 */
    echo "{ \"mailbox\":".json_encode($mailbox)." }";

    $name = "{ \"mailbox\":".json_encode($mailbox)." }";

    $_SESSION['myValue']=$name;
}

?>

On page 2 (test.php):

<?php

session_start();

echo $_SESSION['myValue'];

?>

Here are the entries in the db:

db

This is my logCat:

I/RESPONSE(4591): SELECT * FROM messages WHERE touser='r'

I/RESPONSE(4591): { "mailbox":[{"id":"117","fromuser":"qw","touser":"r","message":"zx","timestamp":"2013-04-13 01:30:59"}] }

Now above I want you to see that touser is r. This entry (in the db) is what I wanted to return, i.e. all entries where the field "touser" contains the value "r".

Now, this is my test.php page:

{ "mailbox":[{"id":"132","fromuser":"r","touser":"","message":"","timestamp":"2013-04-13 15:45:03"},{"id":"123","fromuser":"r","touser":"","message":"","timestamp":"2013-04-13 13:41:23"},{"id":"122","fromuser":"r","touser":"","message":"","timestamp":"2013-04-13 13:30:53"},{"id":"133","fromuser":"","touser":"","message":"","timestamp":"2013-04-13 15:45:21"}] }

EDITED:

As you would see, it returned all touser entries that were blank (or null). My interpretation of this behaviour is that while the code is running, the $from variable is in fact 'r' (and that's why it's saved in logcat correctly), but after the code executes there is presently no value in $from so it just outputs the result for $from = null.

Should php be doing this? I thought that whatever code was executed and echoed at the time would have been output on the page and STAYED there until I re-ran the code. Any suggestions on how to fix this?

EDITED

The point of all this is to use the JSONParser code I got from here, to get particular rows from a database, and put them into the android's database into particular fields. I've tried modifying my JSONParser code to use BackgroundTask like this, and you can take a look at this.

This is what the project is trying to achieve: The first user can post a message to a server db whch contains 'from' (which would be user1's cell number), 'to' (which would be destination or user2's number), and a message (which is his location). The second user uses a polling service to periodically retrieve his messages from the database, i.e. all entries in the database where his number would be in the "to" field and store those entries into a database on his phone (and automatically delete those entries from the server's db). Everything is basically working, I think, except the part where I get particular entries from the server's db.

I decided to use JSONParsing because it can take an array of db entries and make a string out of each entry, making it easier to store in my android's db. But this is where it lacks..finding a way to retrieve particular entries of a db..I have not figured out a way to do this. Any help to point me into the right direction?

Community
  • 1
  • 1
Keegs
  • 95
  • 3
  • 11
  • Given that all of the `touser` in the test.php results are blank it feels like there is no `$from` value... are you certain that you are POSTing a from to your script? – Phill Sparks Apr 13 '13 at 20:38
  • @PhillSparks - Yeah $from does have a value, as can be seen from this line in the logcat: I/RESPONSE(4591): SELECT * FROM messages WHERE touser='r'...the 'r' is the $from. – Keegs Apr 14 '13 at 02:12
  • @PhillSparks - But on the other hand, when I replace '$from' with 'r', in the php code: `$query = "SELECT * FROM messages WHERE touser='$from'";` then test.php outputs the correct thing. – Keegs Apr 14 '13 at 03:52
  • What do you do differently when you use test.php and logCat? Are you posting from Android (I think that's where logCat is?) and just opening test.php in a browser? If you're just opening test.php in a browser then you're not POSTing, so $_POST won't have any values. – Phill Sparks Apr 14 '13 at 08:44
  • @PhillSparks - Ok, well all your questions are correct. And I've modified my Question if you would read the end. – Keegs Apr 14 '13 at 14:16
  • I think this may be an Android/Java problem rather than a PHP problem, let me write up an answer for you rather than trying to explain in a comment. – Phill Sparks Apr 14 '13 at 14:54

2 Answers2

3

Sessions are designed to be unique for each visitor, this is done by remembering the session in a cookie. Your computer will have a different session to your Android. And in fact each browser on your computer will have a different session, unless they have a way of sharing cookies.

Furthermore, scripted requests (like Android code) do not automatically remember cookies and send between requests, and so they may start a new session for each request, and lose information from the previous session.

There are a number of solutions...

  1. Restructure your process so that you do not need to retain the messages in this way - just returning the messages from the script that deletes them maybe? Or having the first script return messages and pass the second script the message IDs to delete. This last point might also be "safer" as you're not deleting anything until you know the app has received it.

  2. Find a way to remember cookies and send them with subsequent requests. How do I make an http request using cookies on Android? might be useful for this.

  3. Get the session ID from PHP and send it along with all your requests. You can get and set the session ID in PHP with session_id().

The last two are not great practice for APIs, as we aim for APIs to be stateless, but they may work for you.

Community
  • 1
  • 1
Phill Sparks
  • 20,000
  • 3
  • 33
  • 46
  • Thanks alot for the explanation. I will investigate, and I am going to implement one of these today so the project can finally be finished! – Keegs Apr 14 '13 at 15:35
0

Yeah i see the issue, thats odd i have not come across this before, but it appears you can add something to your SQL statement to correct it...

SELECT * FROM messages WHERE touser='$from' AND WHERE touser IS NOT NULL

I think that will prevent the null rows from showing. The IS NOT NULL should be the key.

Derple
  • 865
  • 11
  • 28
  • Error message:SELECT * FROM messages WHERE touser=''AND WHERE touser IS NOT NULL database error! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE touser IS NOT NULL' at line 1 – Keegs Apr 13 '13 at 23:50
  • You do not want `WHERE` after `AND`. – Phill Sparks Apr 14 '13 at 08:41
  • @PhillSparks - I know, tried that but still doesn't change anything because like you said `$_POST` wouldn't have any values. – Keegs Apr 14 '13 at 14:18