The componentDidMount() life-cycle hook. This is executed when your React component is inserted/mounted in the DOM.
Installing the Axios Client
$ npm install axios
C:\reactdev\myreactdev>npm install axios
https://github.com/axios/axios
CREATE TABLE `employee` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`position` varchar(100) NOT NULL,
`office` varchar(100) NOT NULL,
`age` int(11) NOT NULL,
`salary` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `employee`
--
INSERT INTO `employee` (`id`, `name`, `position`, `office`, `age`, `salary`) VALUES
(1, 'Tiger Wood', 'Accountant', 'Tokyo', 36, 5689),
(2, 'Mark Oto Ednalan', 'Chief Executive Officer (CEO)', 'London', 56, 5648),
(3, 'Jacob thompson', 'Junior Technical Author', 'San Francisco', 23, 5689),
(4, 'cylde Ednalan', 'Software Engineer', 'Olongapo', 23, 54654),
(5, 'Rhona Davidson', 'Software Engineer', 'San Francisco', 26, 5465),
(6, 'Quinn Flynn', 'Integration Specialist', 'New York', 53, 56465),
(8, 'Tiger Nixon', 'Software Engineer', 'London', 45, 456),
(9, 'Airi Satou updated', 'Pre-Sales Support updated', 'New York', 25, 4568),
(10, 'Angelica Ramos updated', 'Sales Assistant updated', 'New York', 45, 456),
(11, 'Ashton updated', 'Senior Javascript Developer', 'Olongapo', 45, 54565),
(12, 'Bradley Greer', 'Regional Director', 'San Francisco', 27, 5485),
(13, 'Brenden Wagner', 'Javascript Developer', 'San Francisco', 38, 65468),
(14, 'Brielle Williamson', 'Personnel Lead', 'Olongapo', 56, 354685),
(15, 'Bruno Nash', 'Customer Support', 'New York', 36, 65465),
(16, 'cairocoders', 'Sales Assistant', 'Sydney', 45, 56465),
(17, 'Zorita Serrano', 'Support Engineer', 'San Francisco', 38, 6548),
(18, 'Zenaida Frank', 'Chief Operating Officer (COO)', 'San Francisco', 39, 545),
(19, 'Sakura Yamamoto', 'Support Engineer', 'Tokyo', 48, 5468),
(20, 'Serge Baldwin', 'Data Coordinator', 'Singapore', 85, 5646),
(21, 'Shad Decker', 'Regional Director', 'Tokyo', 45, 4545);
ALTER TABLE `employee`
ADD PRIMARY KEY (`id`);
ALTER TABLE `employee`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=22;
//src/index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App />, document.getElementById('root') )public/index.html
//public/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>ReactJS </title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <div id="root"></div> </body> </html>src/App.js
//src/App.js import React, { Component } from 'react'; import axios from 'axios'; class App extends Component { state = { employees: [] } componentDidMount() { const url = 'http://localhost/devtest/reactjs/employees.php/' axios.get(url).then(response => response.data) .then((data) => { this.setState({ employees: data }) console.log(this.state.employees) }) } render() { return ( <div className="container" style={{padding: 20}}> <div className="col-xs-8"> <h1>ReactJS PHP Mysql and Axios List all data - localhost/employees.php</h1> <table className="table table-striped"> <thead className="thead-light "> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Salary</th> </thead> <tbody> {this.state.employees.map((rs, index) => ( <tr key={index}> <td>{rs.name}</td> <td>{rs.position}</td> <td>{rs.office}</td> <td>{rs.age}</td> <td>$ {rs.salary}</td> </tr> ))} </tbody> </table> </div> </div> ); } } export default App;http://localhost/devtest/reactjs/employees.php/
//devtest/reactjs/employees.php <?php header("Access-Control-Allow-Origin: *"); $host = "localhost"; $user = "root"; $password = ""; $dbname = "testingdb"; $id = ''; $con = mysqli_connect($host, $user, $password,$dbname); $method = $_SERVER['REQUEST_METHOD']; if (!$con) { die("Connection failed: " . mysqli_connect_error()); } switch ($method) { case 'GET': $sql = "select * from employee"; break; } // run SQL statement $result = mysqli_query($con,$sql); // die if SQL statement failed if (!$result) { http_response_code(404); die(mysqli_error($con)); } if ($method == 'GET') { if (!$id) echo '['; for ($i=0 ; $i<mysqli_num_rows($result) ; $i++) { echo ($i>0?',':'').json_encode(mysqli_fetch_object($result)); } if (!$id) echo ']'; } elseif ($method == 'POST') { echo json_encode($result); } else { echo mysqli_affected_rows($con); } $con->close();