Note that there are some explanatory texts on larger screens.

plurals
  1. POTake data from one php page and echo it onto another page
    text
    copied!<p>I need to simply take data from one page, upon a condition, and echo it on another page. Here is my code and my other attempts which do not work.</p> <pre><code>&lt;?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 ($_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 { /* user wants to retrieve his messages*/ $query = "SELECT * FROM messages WHERE touser='$from'"; /* echo1 */ echo $query; $result = mysql_query($query) or die("\n\ndatabase error!\n". mysql_error()); $mailbox = array(); while($row = mysql_fetch_assoc($result)){ $mailbox[] = $row; } /* echo2 */ echo "{ \"mailbox\":".json_encode($mailbox)." }"; $name = "{ \"mailbox\":".json_encode($mailbox)." }"; /* echo3 */ echo $name; $_SESSION['myValue']=$name; } ?&gt; </code></pre> <p>And on the next php page, called test.php:</p> <pre><code>&lt;?php session_start; echo $_SESSION['myValue']; ?&gt; </code></pre> <p>My logcat shows that I am getting data in all three echos, but still nothing is being displayed on test.php(i.e. page2).</p> <p>I also tried: </p> <p>Attempt 1: In page 1:</p> <pre><code>session_start(); $_SESSION['myValue']=$name; </code></pre> <p>And in page2 used:</p> <pre><code>session_start(); echo $_SESSION['myValue']; </code></pre> <p>Attempt 2: In page 1:</p> <pre><code>session_start(); $_SESSION['name'] = $name ; </code></pre> <p>And in page2 used:</p> <pre><code>session_start(); $name = $_SESSION['name']; </code></pre> <p>Attempt 3: In page 1:</p> <pre><code>header("Location: test.php?name=".$name); </code></pre> <p>And in page2 used:</p> <pre><code>$name = $_GET['name'] ; </code></pre> <p>But nothing is being displayed on test.php(In this last attempt3 I got a bunch of warnings and maybe errors). What am I doing wrong?</p> <p>This is my logCat:</p> <p>04-13 14:46:22.564: I/RESPONSE(4591): SELECT * FROM messages WHERE touser='r' 04-13 14:46:22.564: I/RESPONSE(4591): { "mailbox":[{"id":"117","fromuser":"qw","touser":"r","message":"zx","timestamp":"2013-04-13 01:30:59"}] }</p> <p>Now above I want you to see that touser is r. This entry into the db is what I want, i.e. all entries where the field "touser" contains the value "r".</p> <p>Now, this is my test.php page:</p> <p>{ "mailbox":[{"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"}] }</p> <p>As you would see, it returned all entries where the fromuser was r. Why do the two contradict each other??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload