Vue.js Fetch Data from MySQL Database using PHP

Getting Started

In this tutorial, I've used Bootstrap CDN, so you need internet connection for it to work.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as crud. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2. `memid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` VARCHAR(30) NOT NULL,
  4. `lastname` VARCHAR(30) NOT NULL,
  5. PRIMARY KEY(`memid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database setup

Inserting Data into our Database

Next. we're gonna insert sample data into our database. These are the data that we are going to fetch in this tutorial. 1. Click database crud that we have created earlier. 2. Click SQL and paste the ff codes:
  1. INSERT INTO `members` (`memid`, `firstname`, `lastname`) VALUES
  2. (1, 'neovic', 'devierte'),
  3. (2, 'gemalyn', 'cepe');

index.php

This is we're we show the data that we are fetching in the form of table.
  1. <!DOCTYPE html>
  2. <title>Vue.js CRUD Series using PHP/MySQLi</title>
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  4. </head>
  5. <div class="container">
  6. <h1 class="page-header text-center">Vue.js CRUD OPERATION with PHP/MySQLi</h1>
  7. <div id="members">
  8. <div class="col-md-8 col-md-offset-2">
  9. <div class="row">
  10. <div class="col-md-12">
  11. <h2>Member List
  12. <button class="btn btn-primary pull-right"><span class="glyphicon glyphicon-plus"></span> Member</button>
  13. </h2>
  14. </div>
  15. </div>
  16. <table class="table table-bordered table-striped">
  17. <th>Firstname</th>
  18. <th>Lastname</th>
  19. <th>Action</th>
  20. </thead>
  21. <tr v-for="member in members">
  22. <td>{{ member.firstname }}</td>
  23. <td>{{ member.lastname }}</td>
  24. <td>
  25. <button class="btn btn-success"><span class="glyphicon glyphicon-edit"></span> Edit</button> <button class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button>
  26.  
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. </div>
  33. </div>
  34. <script src="vue.js"></script>
  35. <script src="axios.js"></script>
  36. <script src="app.js"></script>
  37. </body>
  38. </html>

api.php

This is our PHP API code that contains data from our database.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "crud");
  4.  
  5. if ($conn->connect_error) {
  6. die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. $out = array('error' => false);
  10.  
  11. $crud = 'read';
  12.  
  13. if(isset($_GET['crud'])){
  14. $crud = $_GET['crud'];
  15. }
  16.  
  17.  
  18. if($crud = 'read'){
  19. $sql = "select * from members";
  20. $query = $conn->query($sql);
  21. $members = array();
  22.  
  23. while($row = $query->fetch_array()){
  24. array_push($members, $row);
  25. }
  26.  
  27. $out['members'] = $members;
  28. }
  29.  
  30.  
  31. $conn->close();
  32.  
  33. header("Content-type: application/json");
  34. echo json_encode($out);
  35. die();
  36.  
  37.  
  38. ?>

app.js

Lastly, this is our Vue.js code that fetches data from our API.
  1. var app = new Vue({
  2. el: '#members',
  3. data:{
  4.  
  5. members: []
  6. },
  7.  
  8. mounted: function(){
  9. this.getAllMembers();
  10. },
  11.  
  12. methods:{
  13. getAllMembers: function(){
  14. axios.get("api.php")
  15. .then(function(response){
  16. //console.log(response);
  17. app.members = response.data.members;
  18. });
  19. }
  20. }
  21. });
That ends this tutorial. Stay for the continuation of this vue.js crud operation. Happy Coding :)

Comments

Submitted byLucas Branco (not verified)on Sat, 06/23/2018 - 14:11

I REALLY THANK YOU, GOD BLESS YOU !!!!!!

Add new comment