为什么不PHP页面显示在LogCat中是一回事吗? [英] Why doesn't the php page show the same thing in the LogCat?

查看:223
本文介绍了为什么不PHP页面显示在LogCat中是一回事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的LogCat中显示我(正确),我想返回的所有信息,但是当我问code显示另一个PHP页面上的同样的事情,它会完全怪人。

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.

请注意,它是此code,$ =从R,并以$ =

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

下面是我的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;
}

?>

在第2页(test.php的):

On page 2 (test.php):

<?php

session_start();

echo $_SESSION['myValue'];

?>

下面是在数据库中的条目:

Here are the entries in the db:

这是我的logcat的:

This is my logCat:

I / RESPONSE(4591):SELECT * FROM WHERE消息TOUSER =R

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

的I /响应(4591):{信箱:[{编码:117,FROMUSER:QW,至用户:R,消息:ZX,时间戳:2013年4月13日一时30分59秒}]}

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

现在上面我希望你能看到TOUSER为r。此条目(在DB)是什么,我想回,即,其中场至用户包含值的所有条目的r。

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".

现在,这是我的test.php的页面:

Now, this is my test.php page:

{信箱:[{ID:132,FROMUSER:R,至用户:,消息:,时间戳:2013年4月13日15时45分03秒},{ID:123,FROMUSER:R,至用户:,消息:,时间戳:2013年4月13日13 :41:23},{ID:122,FROMUSER:R,至用户:,消息:,时间戳:2013年4月13日13: 30:53},{ID:133,FROMUSER:,至用户:,消息:,时间戳:2013年4月13日15:45: 21}]}

{ "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"}] }

编辑:

正如你所看到的,它返回的是空白的(或空)所有至用户条目。这种行为我间pretation是,code运行时,从变量$实际上是在'R'(这就是为什么它正确地保存在logcat中),但code执行以后那里为presently从这样$没有价值,它只是从输出为$结果= NULL。

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.

PHP的应该做这个?我想,无论code当时执行,回荡会一直在页面上输出,并在那里呆了,直到我重新跑了code。如何解决这个问题有什么建议?

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

所有这一切的关键是使用JSONParser code从我的这里,从数据库中获取特定行,并把它们放到Android的数据库到特定的领域。我试着改变我的JSONParser code使用BackgroundTask像<一个href=\"http://stackoverflow.com/questions/15917061/why-does-the-asynctask-not-execute-the-doinbackground-jsonobject\">this,你可以看看<一个href=\"http://stackoverflow.com/questions/15866915/how-to-output-string-to-another-php-page-for-jsonparser-request\">this.

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.

这是什么项目要达到的目的:第一个用户可以发布消息到服务器数据库界河包含'从'(这将是user1的手机号码),'到'(这将是目的地或user2的号码)和消息(这是他的位置)。第二个用户使用的查询服务定期从数据库中检索他的消息,即数据库中的所有项目,他的号码将在到字段,这些条目到数据库中存储了他的手机上(并自动删除这些条目服务器的分贝)。一切基本上是工作,我想,除非我得到从服务器的数据库中具体条目的部分。

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.

我决定使用JSONParsing,因为它可以采取DB项的数组,并作出串出每个条目,使其更容易在我的Andr​​oid的分贝存储。但是,这是它lacks..finding一种方法来检索db..I特定条目还没有想出一个办法做到这一点。任何帮助点我到正确的方向?

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?

推荐答案

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

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.

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

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. 重组的过程中,这样你就不需要保留这样消息 - 刚刚从删除它们也许脚本返回的消息?或具有第一脚本返回消息,并通过第二个脚本的消息ID删除。最后一点也可能是安全为你不删除任何内容,直到你知道应用程序已经收到。

  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.

找到一个方式来记住饼干和后续请求给他们。 我如何在Android上使用的cookies的HTTP请求?可能是这个有用的。

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.

从PHP获取会话ID,并与所有请求发送给它一起。你可以和 SESSION_ID设置PHP会话ID()

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().

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

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

这篇关于为什么不PHP页面显示在LogCat中是一回事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆