简体   繁体   English

为什么php页面在LogCat中没有显示相同的内容?

[英]Why doesn't the php page show the same thing in the LogCat?

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. 我的Logcat向我(正确地)显示了我想要返回的所有消息,但是当我要求代码在另一个php页面上显示相同的内容时,它就完全没了。

Note that is this code, $from = "r", and $to = "". 请注意,这是代码,$ from =“r”和$ 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): 在第2页(test.php):

<?php

session_start();

echo $_SESSION['myValue'];

?>

Here are the entries in the db: 以下是db中的条目:

D b

This is my logCat: 这是我的logCat:

I/RESPONSE(4591): SELECT * FROM messages WHERE touser='r' 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"}] } 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. 现在我想让你看到touser是r。 This entry (in the db) is what I wanted to return, ie all entries where the field "touser" contains the value "r". 此条目(在数据库中)是我想要返回的,即字段“touser”包含值“r”的所有条目。

Now, this is my test.php page: 现在,这是我的test.php页面:

{ "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"}] } {“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). 如您所见,它返回所有空白(或null)的touser条目。 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. 我对这种行为的解释是,当代码运行时,$ from变量实际上是'r'(这就是它正确保存在logcat中的原因),但是在代码执行之后,目前在$中没有值,所以它只是输出$ from = null的结果。

Should php be doing this? php应该这样做吗? 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 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. 所有这一切的要点是使用我从这里获得的JSONParser代码,从数据库中获取特定行,并将它们放入android的数据库中的特定字段中。 I've tried modifying my JSONParser code to use BackgroundTask like this , and you can take a look at this . 我试着改变我的JSONParser代码使用BackgroundTask像这样 ,你可以看看这个

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). 这是项目试图实现的目标:第一个用户可以将消息发布到服务器db,其中包含'from'(可以是user1的单元号),'to'(可以是destination或user2的号码),以及消息(这是他的位置)。 The second user uses a polling service to periodically retrieve his messages from the database, ie 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. 我认为,除了从服务器的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. 我决定使用JSONParsing,因为它可以获取一个db条目数组,并从每个条目中生成一个字符串,使其更容易存储在我的android的数据库中。 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. 但这是它缺乏的地方..找到一种方法来检索db的特定条目。我还没有找到一种方法来做到这一点。 Any help to point me into the right direction? 有什么帮助指出我正确的方向?

Sessions are designed to be unique for each visitor, this is done by remembering the session in a cookie. 会话设计为每个访问者都是唯一的,这是通过记住cookie中的会话来完成的。 Your computer will have a different session to your Android. 您的计算机将与Android进行不同的会话。 And in fact each browser on your computer will have a different session, unless they have a way of sharing cookies. 事实上,除非他们有共享cookie的方式,否则计算机上的每个浏览器都会有不同的会话。

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. 此外,脚本请求(如Android代码)不会自动记住cookie并在请求之间发送,因此它们可能会为每个请求启动新会话,并丢失上一个会话中的信息。

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. 或者让第一个脚本返回消息并将第二个脚本传递给要删除的消息ID。 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. 找到一种方法来记住cookie并将其与后续请求一起发送。 How do I make an http request using cookies on Android? 如何在Android上使用Cookie发出http请求? might be useful for this. 可能对此有用。

  3. Get the session ID from PHP and send it along with all your requests. 从PHP获取会话ID并将其与您的所有请求一起发送。 You can get and set the session ID in PHP with session_id() . 您可以使用session_id()在PHP中获取和设置会话ID。

The last two are not great practice for APIs, as we aim for APIs to be stateless , but they may work for you. 最后两个对于API来说不是很好的做法,因为我们的目标是使API成为无状态 ,但它们可能适合您。

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... 是的,我看到了这个问题,奇怪的是我以前没遇到过这个问题,但看起来你可以在你的SQL语句中添加一些内容来纠正它...

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. IS NOT NULL应该是关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM