-2

So i had setup a site which fetches mysql queries and get output json from PHP which then i put to use with js.

So it becomes a 5 step process.

Query -> PDO Statement -> PHP Array -> JSON Strings -> JS Object or AJAX

Step 3-4-5 can be controversial as per developers choice.

I use JSON for mostly lower informative textual content which actually doesn't get valued among security concerns. But to manage a large textual content over ( > 100000) queries, its better to stick with mysql. Mysql is very native to PHP. So why shouldn't we just use php to perform same tasks that we make use for json.

I understand a major block would be ajax calling, but with advent of reactphp web server scripts, async calls can be made [link].

If we can work out in 3 steps using PHP, then is there any absolute reason we should use/recommend JSON?Speed, flexibility, offline-nature?Anything else?

Edit:

How can i reduce the steps to output my mysql query as JSON omitting php json_encode function? CAn it be done directly?

Sachin Kanungo
  • 1,054
  • 9
  • 17
  • I'm confused. Are you saying you want to use MySQL directly in JS? – rjdown Sep 13 '15 at 14:11
  • MYSQL in JSON i said. – Sachin Kanungo Sep 13 '15 at 14:12
  • You don't use JSON to do queries in mysql. Mysql is a DBMS and JSON is used to transmit data, you can not compare them to each other – Lucian Depold Sep 13 '15 at 14:12
  • 2
    And that, Sachin, is why I'm confused. You'd better show some code because it's hard to understand what you're currently doing. It makes no sense. – rjdown Sep 13 '15 at 14:14
  • it's called a mysql client layer. Something has to have it, to talk the talk, walk the walk – Drew Sep 13 '15 at 14:16
  • something like this! http://stackoverflow.com/questions/383631/json-encode-mysql-results – Sachin Kanungo Sep 13 '15 at 14:16
  • `I use JSON blah blah blah, But to manage blah blah, its better to stick with mysql`. WHAT ? – Jigar Sep 13 '15 at 14:16
  • "why shouldn't we just use php to perform same tasks that we make use for json" — That doesn't many any sense. PHP is a programming language. JSON is a text based data format. You're comparing apples and trees. – Quentin Sep 13 '15 at 14:23
  • @SachinKanungo I think you have misunderstood JSON, it is not used for `mysql fetching`. It is just a notation/format, which javascript can easily understand, so people use it for ajax mainly. – Jigar Sep 13 '15 at 14:24
  • Neither i am comparing php nor i misunderstood JSON, the problem is i wanted ajax calling features where i can provide mysql query data in chunks. I used `json_encode` function from php for that purpose. That's what i am comparing. If anyway i can make direct ajax call with PHP would be great – Sachin Kanungo Sep 13 '15 at 14:27
  • That makes even less sense. Ajax specifically refers to using JavaScript to make an HTTP request from the browser without leaving the page. PHP doesn't even run in the browser. – Quentin Sep 13 '15 at 14:32
  • @SachinKanungo "mysql query data" is not something Javascript can understand. That's why you have to convert it into a format that it CAN understand, e.g. JSON. You don't have to use JSON... you can use XML, CSV, or any number of arbitrary formats that you can invent yourself. But JSON is already natively supported by JS so it doesn't require extra processing on the client side. That's one of the main reasons it's used. – rjdown Sep 13 '15 at 14:34
  • when did i said php runs in browser? i use php json_encode function to output mysql content. How come php run in browser relates – Sachin Kanungo Sep 13 '15 at 14:34
  • If at the rightmost end you have AJAX then how do you suggest filling the gap between MySQL and Javascript? – axiac Sep 13 '15 at 14:36
  • why don't we step back to your 5 Step process at the top. How would you **like** it to occur. Show it in your question as an [edit] – Drew Sep 13 '15 at 14:36
  • sir,i updated the post with question – Sachin Kanungo Sep 13 '15 at 14:40
  • I already answered this in my previous comment. – rjdown Sep 13 '15 at 14:41
  • so you want a PHP array to stream back to javascript. Maybe take it up with the **W3C** – Drew Sep 13 '15 at 14:41
  • i have 10 columns in my mysql database which i want to update in browser with small chunks as output. All this process should be taken under AJAX calling(without browser reload). Thats what i wanted – Sachin Kanungo Sep 13 '15 at 14:50
  • well if there is no browser calling, there is always courier pigeon. Throughput is poor I hear – Drew Sep 13 '15 at 14:53
  • i edited the typo. it is without browser reload (Async Request to mysql DB) – Sachin Kanungo Sep 13 '15 at 14:55
  • that's cool. Then this is an AJAX how do I do it question then – Drew Sep 13 '15 at 15:03

2 Answers2

1

"mysql query data", or a PHP array, is not something Javascript can understand. That's why you have to convert it into a format that it CAN understand, e.g. JSON.

You don't have to use JSON... you can use XML, CSV, or any number of arbitrary formats that you can invent yourself. But JSON is already natively supported by JS so it doesn't require extra processing on the client side. That's one of the main reasons it's used.

Either way, you have to convert it somehow

rjdown
  • 9,162
  • 3
  • 32
  • 45
  • got my support. And Steve's answer wasn't too shabby either. – Drew Sep 13 '15 at 14:43
  • So i did. Why do you think i am using mysql query directly in javascript. i did called `using php json encode funtion`. Though it's fine, i agree with edit. – Sachin Kanungo Sep 13 '15 at 14:48
  • My comment was asking what you WANT to do, not what you are doing currently. And from your edit "How can i reduce the steps to output my mysql query as JSON omitting php json_encode function? CAn it be done directly?" it sounds like you are wanting to do exactly that. Unfortunately, you can't. – rjdown Sep 13 '15 at 14:51
  • that's more specific.Thanks – Sachin Kanungo Sep 13 '15 at 14:56
0

It's coming

11.6 The JSON Data Type As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column

:

MySQL json doc

Drew
  • 24,851
  • 10
  • 43
  • 78
Steve E.
  • 9,003
  • 6
  • 39
  • 57