0
<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,OPTIONS');

    $con = mysqli_connect("localhost", "root", "", "demo");

    $select_user = "SELECT * FROM users";
    
    $result = mysqli_query($con, $sql);

    while($row = mysqli_fetch_array($result)) {
       echo $row['name']."".$row['email'];
    }
 ?>

I'm using the above code block to retrieve details from the database in my backend & it will give relevant details. In my frontend,

import React, {Component}from 'react';
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Row, Table, Container, Button } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import './App.css';

export default class Home extends Component {
  constructor(props) {
    super(props);

    this.state = {
      userList: []
    }

    this.displayTableRows = this.displayTableRows.bind(this);
  }

  componentDidMount() {
    var url = "http://localhost:80/PHP_REACT/backend/UserList.php";

    axios.get(url)
      .then(res => { 
          console.log(res.data); // PRINT RESULT (NEED SEPARATE RECODS ROW BY ROW)
        })
        .catch(err => console.log(err));
  }

  render() {
    return (
      <div className="App">
        <Container>
          <Row>
            <Link to={'/Signin'}>
              <Button variant="dark" className="mt-3">
                Logout
              </Button>
            </Link>
            <Table className="mt-4 table">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Name</th>
                  <th>Email</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  
                </tr>
              </tbody>
            </Table>
            </Row>
        </Container>
      </div>
    );
  }
}

I'm using this code block to getting data from the backend. It worked, but it sends all the details as one String. I need to get database records one by one & print them inside a table. Can anyone help with this?

1
  • 3
    Please post your code instead of images Commented Jan 7, 2021 at 5:26

1 Answer 1

1

Return data in json format from php file

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,OPTIONS');

    $con = mysqli_connect("localhost", "root", "", "demo");

    $select_user = "SELECT * FROM users";
    
    $result = mysqli_query($con, $sql);
    
    $data = array()
    while($row = mysqli_fetch_array($result)) {
       array_push($data,$row['name']."".$row['email']); // push data to empty array
    }

    echo json_encode($data);
 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

This works pretty much.. Also asking for a quick favour @RobinSingh.. I need to get two columns from the database(name, email) and pass it to the front end & display it. Any clue of how to get two columns separately & pass them to the front?
@prabathshalitha Just create a array inside while loop and push it. array_push($data, array('your_column' => $your_value, 'your_second_column' => $your_second_value));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.