9951 explained code solutions for 126 technologies


reactjsHow can I use ReactJS and PHP together to develop a web application?


ReactJS and PHP can be used together to develop a web application. ReactJS is used for the front-end development and PHP is used for the back-end development.

To use ReactJS and PHP together, an API should be created in PHP to provide the data to the ReactJS application. The PHP API can communicate with a database and fetch the data requested by the ReactJS application.

For example, the following code creates an API endpoint in PHP that fetches data from a MySQL database and returns it as JSON:

<?php
    // Connect to MySQL
    $db = mysqli_connect("localhost", "username", "password", "database");

    // Fetch the data from the MySQL database
    $result = mysqli_query($db, "SELECT * FROM table");

    // Create an array
    $data = array();

    // Loop through the data
    while ($row = mysqli_fetch_array($result)) {
        $data[] = $row;
    }

    // Return the data as JSON
    echo json_encode($data);
?>

The ReactJS application can then make an HTTP request to the API endpoint and use the returned data.

Here is an example of a ReactJS component that makes an HTTP request to the API endpoint and displays the returned data:

import React, { useState, useEffect } from 'react';

function App() {
  const [data, setData] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch('/api/endpoint');
      const data = await response.json();
      setData(data);
    }
    fetchData();
  }, []);

  return (
    <div>
      {data.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </div>
  );
}

export default App;

The above code will make an HTTP request to the API endpoint and display each item's name in a div.

Helpful links

Edit this code on GitHub