简体   繁体   English

SQL数据和JavaScript-最佳实践

[英]SQL Data & JavaScript - Best Practice

Let's say a website needs to pull information from a specific table in a database based on a user's menu selection. 假设某个网站需要根据用户的菜单选择从数据库的特定表中提取信息。 That table's data is then fed into some JS equations and thrown onto the page. 该表的数据然后被馈送到一些JS方程中,并扔到页面上。

What is the best way to go about pulling that table's information? 提取该表的信息的最佳方法是什么? I've read that trying to access an SQL database via JavaScript is bad practice, so is there another way to do this? 我读过尝试通过JavaScript访问SQL数据库是一种不好的做法,那么还有另一种方法可以做到这一点吗? I know about PHP's json_encode , but I guess I'm not entirely sure 我知道PHP的json_encode ,但我想我不太确定

  1. What the syntax is if I'm calling PHP from a JS script, and 如果我从JS脚本调用PHP,语法是什么,并且
  2. If that's 'best' practice. 如果那是“最佳”实践。 Still relatively new to this, so I'd like to do this right. 对此还是比较新的,所以我想做正确的事。

Another option as far as I'm concerned is attempting to pull ALL of the possible tables (not a security concern) at once on page load. 就我而言,另一种选择是尝试在页面加载时一次拉出所有可能的表(与安全无关)。 I expect that'd introduce a good deal of latency, though. 我希望这会带来很多延迟。

It looks to me that you are not really sure what technique to use. 在我看来,您不太确定要使用哪种技术。 Here are some options. 这里有一些选择。 I'm not going to type them here because, there is enough to find about each one: 我不会在这里键入它们,因为每个元素都有足够的查找空间:

plain php: w3schools 普通php: w3schools

pure ajax call: stackoverflow 纯ajax调用: stackoverflow

jquery: jquery jQuery的: jQuery的

Ajax calls are more user friendly and many times more efficient because, you don't have to refresh the page. Ajax调用更加用户友好,并且效率提高了许多倍,因为您不必刷新页面。 I usually get all information at once( as long your mysql data is not to big). 我通常一次获得所有信息(只要您的mysql数据不大)。 As for security: You use php either way so it doesn't matter if you use Ajax or not. 至于安全性:您可以使用php的任何一种方式,因此无论您是否使用Ajax都无关紧要。 Oh and don't select valuable data of users data (like password or their emails). 哦,不要选择用户数据的有价值的数据(例如密码或他们的电子邮件)。 I hope you get more overview after this :) 希望您在此之后获得更多概述:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM