shall i retrieve database records with JSON ? - php

i'm a little bit confused here. i've PHP file that retrieve database records . i'll call it with an Ajax call from my frontend . do i need to convert the records to JSON ? if no when do i need to do that

You don't "need" to return the results as JSON. But I would recommend it. JSON is very portable, so it will be easier for other applications to interact with your application. It's also much easier to parse JSON than it is records separated by simple delimiters.
For example, you can use Crockford's JSON parser: http://www.json.org/js.html
As for JSON vs XML: Why need to use JSON in php and AJAX

You don't have to use JSON but you can encode any associative array using the function json_encode:
http://php.net/manual/en/function.json-encode.php

If your client is requesting the data in JSON format, then it's probably best to take the results from your database call and convert them to a JSON-formatted string before returning it to your client.
But your client's AJAX call could also be requesting the data in XML format, too.
So the answer depends on what the client is expecting.

Related

Python Beginner: Passing Selenium WebElement Object to PHP

I'm interested in scrapping data from a website, and pass the data for use it in PHP. I have had a look around, and the best suggestion I have been able to find is to first serialise the Python data and then pass it along.
The issue I have is that I'm unsure how to serialise the Python data.
I'm using Selenium, and I have the following code.
test = browser.find_elements_by_css_selector("table#resultstable td")
I can see the data I want to use by running the variable through a loop and printing it.
for val in test:
print(val.text)
However when I try to serialise the object, I receive the following error:
json.dumps(test)
....
TypeError: Object of type 'WebElement' is not JSON serializable!
I Hope somebody can point me in the right direction, I'm happy in PHP but I have only begun to look Python recently.
JSON the data format is limited in what it can store (numbers, strings, booleans, and then arrays or maps (what python calls dictionaries) of these elements (or other arrays or maps which at some point end in one of those formats). json the python library for manipulating json data can therefore only dump to a json string something that fits those rules. In your case, I'd suggest as a simple starting point:
columns = [val.text for val in test] # convert to a list of strings, where each string is the td.text
json.dumps(columns) should then give you something useful

How to pass field values from database hosted on a server to android app using php?

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

encrypted object in mysql [duplicate]

The goal is to unserialize a PHP serialized string and get sutable object in C#
Is there any way to make this possible in C#(.Net)?
To be more specific:
We need to make an application which comunicates (Via HTTP) to specific website which returns the needed information. Fortunately/unfortunately we dont have permission to website so the data (array mostly) that is returned from website is PHP serialized.
I suppose using JSON as an intermediary step could be useful.
You should probably write it to XML or JSON. You can construct your C# object back from the XML
Edit: Looks like there is already a XML serializer for PHP

Is there a way to query JSON in PHP?

I've used Google, Yahoo, AND Bing, but I can't find any good answers. I've seen jLinq, but I want to be able to query JSON in PHP in hopes of having a not an SQL database, but instead all data storage within the filesystem on my server. No, I don't care how bad it sounds.
Ideas nonetheless? I would think that there would be a PHP class on this.
-----EDIT-----
Guys, thanks for your answers so far, but I don't think that json_encode and json_decode are of much use. What I want to be able to do is encode/decode JSON, and be able to search it for specific keys with specific values. Albeit I have PROVEN to myself that I can do so, it's a lot of code for something that should be so simple. Anything else you have in mind?
You can use JsonQ package. This package can query over JSON data like Query Builder.
https://github.com/nahid/jsonq
You can call json_encode to convert your data to a string, and write this string to a file. Then when you want to use it, you read the entire file and call json_decode to convert it back to data. When you're done processing it, repeat the encode/write steps.
But if you have multiple processes doing this, they'll completely overwrite what each other is doing. So it's not a very good way to manage shared data.
You could try JSONPath it allows you to query json with xpath as you would xml.
Look into json_decode. This lets you take JSON as a string and parse it into an object in PHP. You could theoretically store the strings as files, and use file_get_contents to retrieve the string from the file.
You would have to write your own searching/indexing/updating/etc algorithms, but if you don't want to use a real database solution (since you said, No, I don't care how bad it sounds.), then I guess this would work.
To search for values in the object you get from json_decode, look into in_array and array_search

Convert an Object to a String in PHP

I want to convert an Object into a String in PHP. Specifically, I'm trying to take a mysql query response, and I'm trying to convert it into something I can write to a file and use later.
Of course, when you try to write an Object to a file, PHP appropriately yells: Catchable fatal error: Object of class DB_result could not be converted to string in .....
Alternatively, if there is some other way of writing the result of a mysql query to a file, that works too. I'm playing around with a home-brewed caching project :)
Maybe serializing? It will take an object/array and convert it to a string (which can then be un-serialized back later)
json_encode and json_decode will also accomplish many of the properties you are looking for via serialize. The advantage is that you can send JSON-encoded data to a web browser and JavaScript can view and modify properties like a native JavaScript object. In addition, JSON is lighter weight than serialized data because its syntax is a lot more compact.

Categories