D
D
delovoy-mikhail2020-08-09 08:14:15
MySQL
delovoy-mikhail, 2020-08-09 08:14:15

How to properly organize React with MySQL database?

Hello!

Can you please tell me how can I link the work of React and the MySQL database?

I tried to make axios and fetch requests for php files that were located in the root folder, but this always resulted in an error.

The only solution that came to my mind was to upload PHP files to a free hosting and make a request for them using the link. The database also had to be placed on a free hosting.

Most likely my approach is not correct, so I'll ask you for advice. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tikhon Ermakov, 2020-08-10
@the_goldmayer

And let's not make porridge and divide it in parts.
You have PHP, MySQL and React.
Backend : PHP and MySQL.
Front end : React.
Backend
First, I would start with the following line of code. This is for us so that we can easily work with data in the form of JSON.

return <a href="https://www.php.net/manual/ru/function.json-encode.php">json_encode(ARRAY)</a>

Moving on...
Frontend
We have React, we need to get the data somehow!
fetch('/api/someroute')
  .then(response => response.json())
    // Мы получили данные! Теперь можно их использовать, напрмер, setState.
   // Пользуйся всегда console.log чтобы увидеть, какие данные к нам поступают.
  .then(data => console.log(data));

I'll be glad to help. Write!

C
cython, 2020-08-09
@cython

If react is run as a separate dev server, then you need to use CORS , that is, in php you should return headers allowing requests from one domain to another.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question