S
S
SteveJ422021-06-08 08:25:47
PHP
SteveJ42, 2021-06-08 08:25:47

How does PHP code understand where the MySQL database is located?

I want to understand how the connection between PHP and MySQL code goes. Let's say I created a config folder in it with a connection file. But how does the connection itself take place, how does the file understand that the database is on the computer?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Petrov, 2021-06-08
@Recosh

The mysql database by default works through a TCP connection, port 3306. The address (host) 127.0.0.1 or localhost will be specified in the configuration file, which means that we connect to ourselves.

S
Stalker_RED, 2021-06-08
@Stalker_RED

When connecting to the database, you also specify the DSN where the host, database name, login and password.
Connection example from PDO documentation

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

Connection example from the Doctrine documentation
$connectionParams = array(
    'dbname' => 'mydb',
    'user' => 'user',
    'password' => 'secret',
    'host' => 'localhost',
    'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);

R
rostislav-zp, 2021-06-08
@rostislav-zp

this guide helped me. everything is perfectly shown in practice. used to transfer data from arduino with a record to the database, followed by output to a web page

Drawing Highcharts (Highstock) charts from MySQL data

unboxit.ru/blog/63-risuem-grafiki-highcharts-highs...

there are nuances in the code, but after reading the comments under the article, you can easily figure it out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question