I had developed an Application in android in which I fetch some data from MYSQL database and do some operations on it and store it in SQLite.Now I want to send all the data stored in SQLite to MYSQL.I know i need to use JSON there.I will store my result in Cursor say
Cursor c="select * from emp" (just example)
now how i will parse this cursor into String to send it to the php page? and how i will decode it there?? Please help me.
If what you are asking is how you can encode data and send it to php as GET parameter you could use the following:
$encoded=base64_encode(serialize($data))
and then call example.com/?data=$encoded
where $data could be the query result from $data=$sth->fetchAll(PDO::FETCH_ASSOC);
and on the receiving end:
$decoded=unserialize(base64_decode($_GET['data']))
but this will limit you to about 2K of data.
If you can use POST, then use POST.
Related
Hi everyone in the database a column called attachments stores data like this
"a:3:{s:6:\"saveTo\";s:7:\"wpmedia\";s:14:\"attachmentType\";s:6:\"images\";s:11:\"attachments\";a:1:{i:0;a:6:{s:12:\"attachmentId\";i:176165;s:4:\"file\";s:68:\"https://www.yallamission.com/wp-content/uploads/2022/10/MG_00283.jpg\";s:8:\"fileName\";s:12:\"MG_00283.jpg\";s:9:\"thumbnail\";s:76:\"https://www.yallamission.com/wp-content/uploads/2022/10/MG_00283-150x150.jpg\";s:8:\"fileSize\";s:9:\"292.85 KB\";s:8:\"fileType\";s:10:\"image/jpeg\";}}}
i need to fetch the image url only yet i am unable to do it successfully as i don't understand this format
I tried to use php functions and substr() yet the text before and after has dynamic content and no fixed standard.
This is serialized data and you can unserialize them and access the attachments by using unserialize($variable) and access the attachment elements.
Use json_encode instead of serialize when building the data for INSERTing. Then use the JSON functions in SQL (if your MySQL is new enough).
I have a database hosted on server whose field values have to be passed to my app. I would like to do with PHP. But people suggest me to use JSON too. Is JSON required? Please guide me how to pass the field values to android app.
JSON means JavaScript Object Notation and it's just a way of formatting your output in a standard way.
So, if you'd like to pass data from a database to an application, you'd need to implement a small API. This can be done using PHP. At this point, you can access data from your database using a browser and parametrizing your queries using url parameters.
PHP can render the data in a simple HTML table for example, but this is just a way of presenting your data. You can also use JSON.
This means that if you need the badges a user has earned, you'll use something like this:
<link_to_your_api>/index.php?method=getBadges&user=<user>
This in turn, will make a request to the database
<?php
// 1. connect to database
// 2. query for the information
// 3. get the result as array
$result = $db->getData();
echo json_encode($result);
?>
This is just an example, hope it helps.
link to json documentation: http://www.json.org/
You pass the data to your app, when it makes a request to your PHP script. JSON is handy because you can package your data in a format, that is both well readable by humans and machines. You can use the gson library then, to process the JSON data in your app.
Create REST api using any server side script PHP,nodejs or any you like which returns JSON response
call the rest api using http request which returns JSOn text
decode the JSON string to JSON object and use with your android code
In my windows client, I should sent my data to server with JSON like this:
[{"score":"MathJun: 90","shenase":"2981051261"},
{"score":"MathJun: 80","shenase":"2981021192"},
{"score":"ChemJun: 90","shenase":"2981027931"},
{"score":"MathFeb: 90","shenase":"2981060775"},
{"score":"MathJun: 90","shenase":"2981010824"},
{"score":"MathJun: 00","shenase":"2981017039"},
{"score":"ChemJun: 10","shenase":"3120292011"}]
And number of JSON blocks in between 1 to 40.
And in my PHP file, in a For loop I insert a record to my database with data of every JSON blocks.
So the JSON string will be so long. One person told to divide it to 5 parts and send it into 5 GET. Does it really effects?
What is the best solution for this job?
Does then length of JSON string causes problems?
And how should I fix it?
Does executing 40 query in a for loop caused problem?
Some time JSON will create problem, because it follows proper structure and more over its not stranded if you want to send big data better to use POST.
You can refer the bellow link for more details about GET and POST
http://www.w3schools.com/tags/ref_httpmethods.asp
And coming to your problem
If you avoid multiple req's to the server it will in busy mode. Instead of better make send data in single shot and you can insert data using for loop or else like bellow wise,
insert into table1 (First,Last) values ('Fred','Smith'),
('John','Smith'),
('Michael','Smith'),
('Robert','Smith');
I suggest you, use POST and send data in single shot, and crate a string
$str="insert into table1 (First,Last) values ('Fred','Smith'),
('John','Smith'),
('Michael','Smith'),
('Robert','Smith')";
like wise and execute the query. It might be fast.
I have absolutely no Idea what to do so please help me.
I have a Database Table that contains BLOB for Image, text, numeric fields.
I have accomplished to retrieve the data from the Table by sending a request from Javascript to PHP tru AJAX by this steps.
Create a AJAX request from javascript using POST method . I passed the SQL fom the post method.
Process the $_POST["SQL"] and retrieve the data from mysql database. Using mysqli_query.
I use mysqli_fetch_array to convert the result to an associative array.
I use echo json_encode to have a JSON string as an output then echo it.
My problem starts here since the field that contains the BLOB image has 'null' value while other fields behave perfectly fine.
Take note that I'm not only retrieving the Image field but multiple fields.
I'm new to web development and I am using Javascript( not Jquery ) and PHP.
What should I do?
Thank you in advance!
I have to list the table entries from mysql in my iPhone app when a button is pressed. I am able to do it properly but it is just not the way I want it. I am using a PHP script to send Xcode what has to be printed.
First, I tried doing it using HTML table, but I didn't like the way it was printed with cells. Next I tried printing plain text by giving spaces(between columns) and \n for every new row. I used NSURL and loaded the webView to the iPhone. Looks good on browser but the same is not preserved when the iPhone tries to open it.
Is there a good way to do this? So I can just list the table entries without having to go through the traditional HTML table or any other idea is welcome.
Also, please try to be easily understood, as I am new to Obj-C, and PHP as well.
Thanks!!
Any thoughts on how I can do this in a UITableView..?? Do I have to return a string with component separation characters and fill in the tableView?
Output the results encoded in JSON. Send an a(sync) request to the server using NSURLConnection or using a third-party library such as AFNetworking. Parse the JSON using NSJSONSerialization, turning the results into an array/dictionary depending on the contents. Then parse the results into the UITableViewCell. It may be easier to subclass the cell so that you include the data that you'd like to use.
To encode the results from the database into JSON, you can use the method json_encode().