Hey everyone!! i want to convert my sql query to JSON and then pass the JSON to the other page. How can i do this ??? Plzz help me…
Yes! LOL, you can do ANYTHING in programming… Just sometimes a puzzle to get it working…
First, show us your current query and how it is used. Place your code inside the PHP tags above and only give us enough code to get an idea of what you want. And, elaborate what you mean by sending the query to another page. It can be done, but, not sure exactly what you are thinking…
Thank you sir for replying. I am creating a login/registration form, i am running a query in which the categories which the user selected at the time of registration are displayed when he logs-in. Now i want to convert it in JSON and pass it to the other page. Here is the code
[php]<?php
include"config.php";
/$Login=$_POST[‘Login’];
if($Login){/
$username=$_POST[‘uname’];
$password=md5($_POST[‘pass’]);
$query2=mysql_query("SELECT ue.id,ue.entityid,u.uname,e.display
FROM userentity ue,userinfo u,entity e
WHERE ue.id = u.id
AND ue.entityid = e.id AND uname=’$username’ AND pass=’$password’ ");
$rows[]=array();
if(mysql_num_rows($query2)){
while($data2=mysql_fetch_array($query2)){
$rows[‘name’][]=$data2;
session_start();
$_SESSION[‘uname’]=$data2[“uname”];
foreach($data2[“display”] as $value){ }
$_SESSION[‘valid_time’] = time();
header(“location: members.php”);
exit;
}
header(“location: index.php”);
}
?>[/php]
Well, Aneeb, there are more than one way depending on why you want to send it to the other page in JSON format. I will assume you are talking about the the row of data from the query. Normally, you just use that in your PHP code and do not need put it into JSON. In the sample code you posted, you are counting the results of the query before you run the query. And, you do not need the “while” to fetch the array of data. Your code:
$rows[]=array();
if(mysql_num_rows($query2)){
while($data2=mysql_fetch_array($query2)){
$rows[‘name’][]=$data2;
Is usually done something more like this:
[php]
$rows = mysql_query($query2);
if(mysql_num_rows($rows)){
$data2=mysql_fetch_array($rows);
}
[/php]
You will end up with all the data from the query in the array $data2.
Now, usually this query is done on the page where you are going to use that data. But, you can send it to another page if needed by several ways. One of the easiest is to use SESSION variables and just save it in a session variable and read the variable on the next page. But, if you are only sending the data to one page, it would be best just to place the above query on the second page. Then, no need to transfer it to the second page. Just put the above code IN the second page. Do the query there.
But, if you must send it to more than one page, do it this way with session variables.
At the top of the PHP code, place session_start(); Place that line on both pages.
In the above page also put $_SESSION[‘data2’] = $data2; after $data2 has been created.
This will place the value of $data2 inside the session variable of the same name.
Then, on the other page, use these lines:
session_start();
$data2 = $_SESSION[‘data2’];
this will read the session varaible and create an array called $data2 in the second page holding the data.
Now to convert to JSON, you can use this line:
$data2_json = json_encode($data2); (The name of the new variable can be whatever you want.)
After that is used, the new variable is in the JSON format.
Now sure, but, I think that is what you wanted… Good luck!
Thank you ErnieAlex, your code works. But the format in which it is converting in json is slight different from the one which i wants. The json is…
{“name”:{“1”:“Promotion”,“2”:“Shopping guide”,“3”:“Supply chain”}}
and i want it like this…
{“name”:{ [ “1”:“Promotion”,“2”:“Shopping guide”,“3”:“Supply chain”} ] }
How can i get this format???
You can use STR_REPLACE to remove the inner brackets…
$x=’{“name”:[{“a”:“Promotion”},{“a”:“Shopping guide”},{“a”:“Supply chain”}]}’
$x=str_replace("},{", “,”, $x);
This would replace the },{ with just the comma and give you the correct results…
Good luck!