为什么不PHP页面显示在logcat的是一回事吗?(Why doesn't the php

2019-10-18 00:24发布

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:

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?

Answer 1:

会议旨在成为每个访客独特的,这是由一个cookie记住会话完成。 您的计算机将有一个不同的会话到Android。 而事实上,在您的计算机上的每个浏览器都会有不同的会话,除非他们有分享饼干的方法。

此外,脚本请求(如Android代码)不会自动记住饼干和请求之间发送,所以他们可能会开始为每个请求一个新的会话,并从以前的会话丢失信息。

有一些解决方案...

  1. 调整你的过程,这样你就不需要保留这样的消息 - 刚刚从删除它们也许脚本返回的消息? 或具有第一脚本返回消息,并通过第二个脚本的消息ID删除。 最后这一点也可能是“安全”为你不删除任何内容,直到你知道该应用程序已收到。

  2. 找到一种方式来记住饼干,并与随后的请求发送给他们。 我如何在Android上使用的cookies的HTTP请求? 可能是这个有用的。

  3. 从PHP获取会话ID,并与所有请求发送沿。 您可以获取和设置会话ID在PHP 的session_id() 。

最后两个是不是API的伟大实践,为我们的目标是API的是无状态的 ,但他们可能会为你工作。



Answer 2:

是的,我看到的问题,这就是奇我还没有遇到过这一点,但它似乎你可以添加一些SQL语句来纠正它...

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

我认为,这将阻止显示空行。 该IS NOT NULL应该是关键。



文章来源: Why doesn't the php page show the same thing in the LogCat?