Preparatory Manipulations for Working with SQL in PHP
Let's now learn how to work with databases through PHP. For this, first of all, it is necessary to establish a connection to the database server.
This is done using the mysql_connect function,
which takes 3 parameters: the hostname
(server), the username under which
we work with the database, and the password for this user.
If you are working on your own computer, then
these will be localhost, root and
an empty string for the password (on some
servers it might also be root).
If your database is on the internet - then this
data is provided to you by your hosting provider.
So, let's establish a connection to the database:
<?php
$host = 'localhost'; // hostname
$user = 'root'; // username
$pass = ''; // password
$name = 'mydb'; // database name
$link = mysqli_connect($host, $user, $pass, $name);
?>
If the access credentials we provided are correct, then
a connection to the database will be established.
In this case, a special connection object will be stored
in the variable $link, which we
will use for all subsequent
interactions with our database.