r/web_programming • u/[deleted] • Sep 19 '16
Javascript: get MySQL
Noob question here.
I'm starting some front end development. I used PHP to scrap some data, and put it into a MySQL database. I'm using Chart.js to display the data.
I want to use PHP to retrieve data from MySQL, then pass it to Javascript; but I'm not sure what I should be doing -- exactly. (Yes, I did some searches and tried a bunch of stuff.)
Some pointers would be appreciated. Thanks.
3
Upvotes
1
u/smacky_face Sep 19 '16
There's very little reason to write code to access a database from scratch. There's a lot of benefit to using an ORM (stands for object relational mapping) library to access your data in MySQL.
The basics: an ORM allows you to access data in a database and treat it like data within your code (php in this case). For example, you might be able to pull a single record and then interact with it like a php object, then call a built-in save method to save it to the database.
It's possible to write SQL queries directly in php. Here are a couple of reasons not to:
You'd be reinventing the wheel
You'll be tightly bound to a particular database. If you want to switch to SQLite down the road for example then you're rewriting every single query. An ORM will avoid this and allow you to just switch adapters instead.
Writing SQL is super annoying
Any major PHP framework has its own built-in ORM - you can look into Laravel or CakePHP or CodeIgniter for a few options. They're focused on web development so if that isn't what you're doing then they'll have a lot you don't need. There should be some decent standalone ORMs out there though - just google "PHP ORM" and see what you get.