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.
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.
1
u/SimonOrJ Sep 19 '16
You're correct that you should use PHP to retrieve data from MySQL. You'll need to use JavaScript to get the data from the PHP.
What I did (unlike smacky_face's recommendation) is learn to write in SQL. Once you get the hang of it, it's not really hard to write it. For example, in your database, your table named "fruits" has columns named "name" and "color". When you run
You'll end up with this table (if you have only two rows in the table):
You can also run:
and you will get:
From here, you can either use PDO or MySQLi to allow PHP to access MySQL database. PDO can connect to MySQL, SQLite (.db files), and many other databases while MySQLi can only connect to MySQL database. You can check this w3schools steps for a tutorial on using PHP to access data from MySQL. More importantly, for your purposes, check this toturial on selecting data (retrieving data) for use in PHP.
After this, it's just the matter of putting the results into an array and using json_encode() to encode it into JSON. You will echo that into the body of the document.
The above will return:
Once you get that code set up, you will need to use JavaScript's AJAX function to make PHP parse MySQL data into JSON for the JavsScript to download. Here's an example in JQuery:
This should write [Array[2], Array[2]] onto the console, which you can expand to see its contents.
I've literally lived through this when I was writing this project for one year: https://github.com/SimonOrJ/CoreProtect-Lookup-Web-Interface