Answer the question
In order to leave comments, you need to log in
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
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>
fetch('/api/someroute')
.then(response => response.json())
// Мы получили данные! Теперь можно их использовать, напрмер, setState.
// Пользуйся всегда console.log чтобы увидеть, какие данные к нам поступают.
.then(data => console.log(data));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question