how to connect to mysql in php

To connect to MySQL using PHP, you can use the mysqli or PDO extension. Here's an example using mysqli:

main.php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
314 chars
14 lines

In this example, replace localhost with your MySQL server name, username and password with your MySQL username and password, myDB with your database name.

Note that you should never store your MySQL credentials in plain text in your PHP code. It's a good practice to keep them in a separate configuration file and load them securely.

gistlibby LogSnag