Connecting PHP to MySQL

Tiempo de lectura: < 1 minuto

Reading time: < 1 minute

To connect PHP to MySQL, you can use the mysqli_connect() function. This is a function from the MySQLi (MySQL Improved) extension in PHP, which is used to establish a connection with a MySQL database.

Here’s an example of how to connect PHP to MySQL:

<?php
$server = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';

$conn = mysqli_connect($server, $username, $password, $database);

if (!$conn) {
    die("Connection error: " . mysqli_connect_error());
}
echo "Connection successful";
?>

In this example, a connection is established with a MySQL database on the same server where the PHP script is located.

If you want to connect to a database on a remote server, you should provide the IP address or hostname of the server instead of localhost.

It’s important to note that you need to provide the username and password of a MySQL user with permissions to access the database.

Additionally, you should replace database with the name of the database you want to connect to.

Leave a Comment