通过 PHP 和 jQuery 将 MySQL 解析为 JavaScript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8334438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:05:05  来源:igfitidea点击:

Parsing MySQL into JavaScript Arrays through PHP and jQuery

phpjavascriptjquerymysql

提问by Jeremy

Ultimate Goal: I want to take data I keep in a MySQL Database and put it into an Array of Arrays in JavaScript, so it can be manipulated client side.

最终目标:我想将我保存在 MySQL 数据库中的数据放入 JavaScript 中的数组数组中,以便可以在客户端进行操作。

So far, I have been able to pull data from my database with this code:

到目前为止,我已经能够使用以下代码从我的数据库中提取数据:

<?php
...
$num=1;

$q = "SELECT blah1, blah2, blah3 WHERE blah4=$num";

$sth = mysqli_query ($database, $q);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
    $rows[] = $r;
    }

print json_encode($rows);

?>

(from: JSON encode MySQL results)

(来自:JSON 编码 MySQL 结果

This is where I am stuck, because I am having trouble getting this data into JavaScript.

这就是我卡住的地方,因为我无法将这些数据导入 JavaScript。

One solution I can find is Parsing a Separate PHP Array in Javascript:

我能找到的一种解决方案是在 Javascript 中解析单独的 PHP 数组

<script>
     var jsonarray = <?php echo json_encode($array); ?>;
     // now you can use jsonarray in your javascript
</script>

But the problem with this implementation is that it would spit out all of the database content I query onto the source-code of the page. If I have to do this, I might as well skip the database and just keep all of the content in javascript.

但是这个实现的问题是它会将我查询的所有数据库内容吐出到页面的源代码上。如果我必须这样做,我不妨跳过数据库并将所有内容保留在 javascript 中。

I am thinking there must be a way with jQuery/AJAX to pass the value of $num to a PHP script, grab this data and put it into a JavaScript Array without having to output all of it to the page.

我认为 jQuery/AJAX 必须有一种方法可以将 $num 的值传递给 PHP 脚本,获取这些数据并将其放入 JavaScript 数组中,而不必将其全部输出到页面。

Any assistance would be appreciated.

任何援助将不胜感激。

Thank you!

谢谢!

回答by Abhi Beckert

This solution you posted:

您发布的此解决方案:

<script>
  var jsonarray = <?php echo json_encode($array); ?>;
  // now you can use jsonarray in your javascript
</script>

Is actually a very good approach. Using AJAX is drastically slower (because of network latency).

其实是一个很好的办法。使用 AJAX 的速度要慢得多(因为网络延迟)。

Unless you really need AJAX for some reason, you should avoid using it. It will add a noticeable split second of load time to the page, often for no benefit at all.

除非出于某种原因你真的需要 AJAX,否则你应该避免使用它。它会给页面增加明显的一秒加载时间,通常根本没有任何好处。

Above all when structuring your page, you want to try and reduce the number of individual network requests between the browser and the server. The less requests the faster your page will be. This is especially true for javascript and ajax, because they are unpredictable and browsers find it very difficult to optimise any part of the page where it's being used.

最重要的是,在构建页面时,您希望尝试减少浏览器和服务器之间的单个网络请求的数量。请求越少,您的页面就会越快。这对于 javascript 和 ajax 尤其如此,因为它们是不可预测的,浏览器发现很难优化正在使用它的页面的任何部分。

We're talking about one quarter of a second compared to one millionth of a second, for exactly the same end result.

我们谈论的是四分之一秒与百万分之一秒相比,最终结果完全相同。

回答by Carsten

Making an AJAX call with jQuery is easy enough. Take a look at the documentation: http://api.jquery.com/jQuery.get/

使用 jQuery 进行 AJAX 调用非常简单。看一下文档:http: //api.jquery.com/jQuery.get/

回答by alex

You can also do this without AJAX

您也可以在没有 AJAX 的情况下执行此操作

var jsonarray = eval(<?php echo json_encode($array); ?>);