mysql connect to database code example

Example 1: php sql connection

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn= mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully.";
?>

Example 2: mysql login console

mysql -u USERNAME -p

Example 3: mysql cli connect with password

mysql --user=root --password=mypass

Example 4: how to connect mysql local server

\connect root@localhost:3306

Example 5: mysql connect

$mysqli -> new mysqli(host, username, password, dbname, port, socket)

Example 6: login to database mysql terminal

use DBNAME;